Last active
December 31, 2015 17:48
-
-
Save JoshuaSullivan/80e8d53505dbe2f0868f to your computer and use it in GitHub Desktop.
Swift's didSet property observer is a great way to dynamically configure a view at runtime, but there are limits to what you should do with it. Read the blog post here: http://www.chibicode.org/?p=32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
@IBOutlet weak var outputLabel: UILabel! { | |
didSet { | |
// Ensure that the label wasn't just set to nil. | |
guard let outputLabel = self.outputLabel else { return } | |
// Set the text color based on the user's style choices. | |
outputLabel.textColor = StyleManager.sharedManager().outputLabelColor | |
// Set the label to use fixed-width numbers. | |
let oldDescriptor = outputLabel.font.fontDescriptor() | |
let fontAttributes = [ | |
UIFontDescriptorFeatureSettingsAttribute : [ | |
UIFontFeatureTypeIdentifierKey : kNumberSpacingType, | |
UIFontFeatureSelectorIdentifierKey : kMonospacedNumbersSelector | |
] | |
] | |
let newDescriptor = oldDescriptor.fontDescriptorByAddingAttributes(fontAttributes) | |
outputLabel.font = UIFont(descriptor: newDescriptor, size: 0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment