Skip to content

Instantly share code, notes, and snippets.

@samhenrigold
Created July 25, 2023 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhenrigold/7bff411cf684c48db5297cdae74eb130 to your computer and use it in GitHub Desktop.
Save samhenrigold/7bff411cf684c48db5297cdae74eb130 to your computer and use it in GitHub Desktop.
System font with high legibility features
// From https://github.com/anonymouscamera/anonymous-camera/blob/b7ae19b07a476c0a8a54274fdaadd9e5ecd811d5/anonymous-camera/Helpers/Helpers.swift
extension UIFont {
static func roundedSystemFont (ofSize fontSize : CGFloat, andWeight weight: UIFont.Weight) -> UIFont {
let systemFont = UIFont.systemFont(ofSize: fontSize, weight: weight)
var font: UIFont = systemFont
if #available(iOS 13.0, *) {
if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
font = UIFont(descriptor: descriptor, size: fontSize)
}
}
return font
}
func fontWithFeature(key: Int, value:Int) -> UIFont {
let originalDesc = self.fontDescriptor
let features:[UIFontDescriptor.AttributeName: Any] = [
UIFontDescriptor.AttributeName.featureSettings : [
[
UIFontDescriptor.FeatureKey.featureIdentifier: key,
UIFontDescriptor.FeatureKey.typeIdentifier: value
]
]
]
let newDesc = originalDesc.addingAttributes(features)
return UIFont(descriptor: newDesc, size: 0.0)
}
func fontWithVerticallyCenteredColon() -> UIFont {
return self.fontWithFeature(key: kStylisticAlternativesType, value: kStylisticAltThreeOnSelector)
}
func fontWithSlashedZero() -> UIFont {
return self.fontWithFeature(key: kTypographicExtrasType, value: kSlashedZeroOnSelector)
}
func fontWithMonospacedNumbers() -> UIFont {
return self.fontWithFeature(key: kNumberSpacingType, value: kMonospacedNumbersSelector)
}
func fontWithHighLegibility() -> UIFont {
return self.fontWithFeature(key: kStylisticAlternativesType, value: kStylisticAltSixOnSelector)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment