Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Last active December 29, 2017 18:11
Show Gist options
  • Save liamnichols/56736b4988c57a33ad70086a0dc6018b to your computer and use it in GitHub Desktop.
Save liamnichols/56736b4988c57a33ad70086a0dc6018b to your computer and use it in GitHub Desktop.
An extension to simplify use of small caps within a UIFont object in Swift.
public extension UIFont {
/// Helper method to create a UIFont with updated attributes applied to the UIFontDescriptor
///
/// - parameter attributes: The new attributes to apply to the fontDescriptor
///
/// - returns: A UIFont object with the new attributes appended to the receivers fontDescriptor
func addingAttributes(_ attributes: [String : Any] = [:]) -> UIFont {
return UIFont(descriptor: fontDescriptor.addingAttributes(attributes), size: pointSize)
}
/// Returns a UIFont object based on the receiver with small caps applied to upper case letters
var addingUpperCaseSmallCaps: UIFont {
return addingAttributes([
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kUpperCaseType,
UIFontFeatureSelectorIdentifierKey: kUpperCaseSmallCapsSelector
]
]
])
}
/// Returns a UIFont object based on the receiver with small caps applied to lower case letters
var addingLowerCaseSmallCaps: UIFont {
return addingAttributes([
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kLowerCaseType,
UIFontFeatureSelectorIdentifierKey: kLowerCaseSmallCapsSelector
]
]
])
}
}
@jlagrone
Copy link

I thought I had this working in an iOS app, but now see it not working after updating to Xcode 8.3. Did something change in Xcode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment