Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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
]
]
])
}
}

Nice one. Why not naming functions "add_" instead of "adding_"?
It's a pretty pedantic observation but the ing form doesn't sound idiomatic in UIKit.

Owner

liamnichols commented Sep 11, 2016

Thanks for the feedback @funkyboy!

I wasn't too keen on it myself also however I decided to just try and keep things consistent with the UIFontDescriptor addingAttributes(_:) method that the extension is based on.

Something like addUpperCaseSmallCaps sounds to me as if i'd be mutating the existing property so I didn't go with that either but the original ObjC fontByAddingUpperCaseSmallCaps: style doesn't really cut it in Swift 3 land anymore :/

I guess a better solution might be to just come up with something completely different maybe?

withUpperCaseSmallCaps?

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