Created
March 17, 2017 14:13
-
-
Save sajadabedi/b8792dc84906a605106eb2c13b544e48 to your computer and use it in GitHub Desktop.
Handling UIFont 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
//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