Skip to content

Instantly share code, notes, and snippets.

@sajadabedi
Created March 17, 2017 14:13
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 sajadabedi/b8792dc84906a605106eb2c13b544e48 to your computer and use it in GitHub Desktop.
Save sajadabedi/b8792dc84906a605106eb2c13b544e48 to your computer and use it in GitHub Desktop.
Handling UIFont in swift
//custom-dynamic-type.swift
protocol CustomDynamicTypeable {
func preferredFont(forTextStyle style: UIFontTextStyle) -> UIFont?
func font(forSize pointSize: CGFloat) -> UIFont?
}
// judson-enum.swift
enum JudsonFont: String, CustomDynamicTypeable {
case regular = "Judson-Regular"
case bold = "Judson-Bold"
}
// dynamic-type-extension-raw-representable.swift
extension CustomDynamicTypeable where Self: RawRepresentable, Self.RawValue == String {
func preferredFont(forTextStyle style: UIFontTextStyle) -> UIFont? {
let font = UIFont.preferredFont(forTextStyle: style)
return UIFont(name: rawValue, size: font.pointSize)
}
func font(forSize pointSize: CGFloat) -> UIFont? {
return UIFont(name: rawValue, size: pointSize)
}
}
// usage
let font = JudsonFont.regular.preferredFont(forTextStyle: .body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment