Last active
May 21, 2019 02:20
-
-
Save samuel-mellert/a29a46a55b749a45f397 to your computer and use it in GitHub Desktop.
Extension to UIFont that allows to use UIFont.systemFontOfSize:weight: on iOS >= 7
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
// | |
// UIFont+SizeAndWeight.swift | |
// | |
// Created by Samuel Mellert on 08/01/16. | |
// | |
import UIKit | |
public enum FontWeight { | |
case Normal | |
case Regular | |
case Bold | |
case Black | |
case Heavy | |
case Semibold | |
case Thin | |
case Light | |
case UltraLight | |
} | |
extension UIFont { | |
@available(iOS 7, *) | |
public class func systemFontOfSize(size: Double, weight: FontWeight) -> UIFont { | |
if #available(iOS 8.2, *) { | |
let fontWeightFloat: CGFloat | |
switch weight { | |
case .UltraLight: | |
fontWeightFloat = UIFontWeightUltraLight | |
case .Light: | |
fontWeightFloat = UIFontWeightLight | |
case .Thin: | |
fontWeightFloat = UIFontWeightThin | |
case .Normal: | |
fontWeightFloat = UIFontWeightRegular | |
case .Regular: | |
fontWeightFloat = UIFontWeightMedium | |
case .Semibold: | |
fontWeightFloat = UIFontWeightSemibold | |
case .Bold: | |
fontWeightFloat = UIFontWeightBold | |
case .Heavy: | |
fontWeightFloat = UIFontWeightHeavy | |
case .Black: | |
fontWeightFloat = UIFontWeightBlack | |
} | |
return UIFont.systemFontOfSize(CGFloat(size), weight: fontWeightFloat) | |
} else { | |
let systemFontName: String | |
switch weight { | |
case .UltraLight: | |
systemFontName = "HelveticaNeue-UltraLight" | |
case .Light: | |
systemFontName = "HelveticaNeue-Light" | |
case .Thin: | |
systemFontName = "HelveticaNeue-Thin" | |
case .Normal: | |
systemFontName = "HelveticaNeue" | |
case .Regular: | |
systemFontName = "HelveticaNeue-Medium" | |
case .Semibold: | |
systemFontName = "HelveticaNeue-Medium" | |
case .Bold: | |
systemFontName = "HelveticaNeue-Bold" | |
case .Heavy: | |
systemFontName = "HelveticaNeue-Bold" | |
case .Black: | |
systemFontName = "HelveticaNeue-Bold" | |
} | |
return UIFont(name: systemFontName, size: CGFloat(size))! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment