// 字體寬度枚舉
enum fontWeight {
case thin
case regular
case medium
case semibold
case bold
@available(iOS 8.2, *)
func systemWeight() -> UIFont.Weight {
switch self {
case .thin:
return UIFont.Weight.thin
case .regular:
return UIFont.Weight.regular
case .medium:
return UIFont.Weight.medium
case .semibold:
return UIFont.Weight.semibold
case .bold:
return UIFont.Weight.bold
}
}
}
// UIFont + Extension
extension UIFont {
/// 系統(tǒng)字體芥牌,默認字號16兄一,Weight為regular
class func font(size: CGFloat = 16, weight: fontWeight = .regular) -> UIFont! {
if #available(iOS 8.2, *) {
return UIFont.systemFont(ofSize: size, weight: weight.systemWeight())
} else {
return UIFont.systemFont(ofSize: size)
}
}
}
使用
label.font = UIFont.font(size: 21, weight: .medium)