Skip to content

Instantly share code, notes, and snippets.

@mjhb
Forked from liamnichols/SmallCaps.swift
Last active December 29, 2017 18:20
Show Gist options
  • Save mjhb/c2051393ce677ecfc044731bdd689c2d to your computer and use it in GitHub Desktop.
Save mjhb/c2051393ce677ecfc044731bdd689c2d to your computer and use it in GitHub Desktop.
An extension to simplify use of small caps within a UIFont object in Swift. Updated for Xcode-9.2
import UIKit.UIFont
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: [UIFontDescriptor.AttributeName : 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([
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kUpperCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kUpperCaseSmallCapsSelector
]
]
])
}
/// Returns a UIFont object based on the receiver with small caps applied to lower case letters
var addingLowerCaseSmallCaps: UIFont {
return addingAttributes([
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector
]
]
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment