Last active
December 29, 2017 18:11
-
-
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.
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
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 | |
] | |
] | |
]) | |
} | |
} |
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
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.