Skip to content

Instantly share code, notes, and snippets.

Last active December 31, 2015 17:48
What would you like to do?
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:
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