Skip to content

Instantly share code, notes, and snippets.

@keehun
Last active February 18, 2019 14:44
Show Gist options
  • Save keehun/d2f2488fab89ea23a208c15971214f76 to your computer and use it in GitHub Desktop.
Save keehun/d2f2488fab89ea23a208c15971214f76 to your computer and use it in GitHub Desktop.
Practical Dynamic Type, Part 3: Attributed Strings
@objc func uiContentSizeCategoryChanged() {
/// Only continue if the `UILabel` has non-nil attributed text.
guard let attributedString = display.attributedText else {
return
}
/// Get the NSRange of the plaintext string.
let fullTextRange = NSRange(location: 0, length: attributedString.string.count)
/// Enumerate over each of the sub attributed strings.
mutableText.enumerateAttributes(in: fullTextRange, options: []) { attributes, range, stop in
print(attributes[.font])
}
/// Optional(<UICTFont: 0x7f8a26429720> font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 18.00pt)
/// Optional(<UICTFont: 0x7f8a26403c80> font-family: ".SFUIText-Light"; font-weight: normal; font-style: normal; font-size: 8.00pt)
/// Optional(<UICTFont: 0x7f8a26429c30> font-family: "NeutrafaceSlabText-Demi"; font-weight: bold; font-style: normal; font-size: 18.00pt)
/// Optional(<UICTFont: 0x7f8a2642aa30> font-family: ".SFUIText-Heavy"; font-weight: bold; font-style: normal; font-size: 48.00pt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment