Last active
February 7, 2017 19:55
-
-
Save meteochu/cdbad8b2d8dbc5f4777035024d958528 to your computer and use it in GitHub Desktop.
UIFont extension for UIFontTextStyle and UIFontWeight
This file contains hidden or 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
import UIKit | |
extension UIFont { | |
enum TextStyle: String { | |
case title1 | |
case title2 | |
case title3 | |
case headline | |
case subheadline | |
case body | |
case callout | |
case footnote | |
case caption1 | |
case caption2 | |
var rawValue: String { | |
let capitalizedString = "\(self)".capitalized | |
return "UICTFontTextStyle\(capitalizedString)" | |
} | |
} | |
enum Weight { | |
case ultraLight | |
case thin | |
case light | |
case regular | |
case medium | |
case semibold | |
case bold | |
case heavy | |
case black | |
var floatValue: CGFloat { | |
switch self { | |
case .thin: return UIFontWeightThin | |
case .light: return UIFontWeightLight | |
case .ultraLight: return UIFontWeightUltraLight | |
case .black: return UIFontWeightBlack | |
case .regular: return UIFontWeightRegular | |
case .bold: return UIFontWeightBold | |
case .medium: return UIFontWeightMedium | |
case .semibold: return UIFontWeightSemibold | |
case .heavy: return UIFontWeightHeavy | |
} | |
} | |
} | |
class func preferredFont(for textStyle: UIFont.TextStyle) -> UIFont { | |
return preferredFont(forTextStyle: textStyle.rawValue) | |
} | |
class func systemFont(ofSize size: CGFloat, weight: UIFont.Weight) -> UIFont { | |
return systemFont(ofSize: size, weight: weight.floatValue) | |
} | |
class func monospacedDigitSystemFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont { | |
return monospacedDigitSystemFont(ofSize: fontSize, weight: weight.floatValue) | |
} | |
@available(iOS 10.0, *) | |
class func preferredFont(for textStyle: UIFont.TextStyle, compatibleWith traitCollection: UITraitCollection?) -> UIFont { | |
return preferredFont(forTextStyle: textStyle.rawValue, compatibleWith: traitCollection) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment