Last active
October 10, 2019 17:26
-
-
Save LukeSmith16/d8d1a2690cf0ee3c00fd1858fa2ac824 to your computer and use it in GitHub Desktop.
Font - As part of implementing a mobile design system interacting with fonts is a key element. This is a little wrapper around UIFont and makes managing fonts in your application a lot more easier and has a lot of room to scale with the project.
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
import UIKit | |
struct Font { | |
enum FontType { | |
case installed(FontName) | |
case custom(String) | |
case system | |
case systemBold | |
case systemItatic | |
case systemWeighted(weight: Double) | |
case monoSpacedDigit(size: Double, weight: Double) | |
} | |
/// These would be custom fonts that you had to add to your project manually... | |
enum FontName: String { | |
case HelveticaRegular = "Helvetica-Regular" // Using Helvetica just as an example | |
case HelveticaBold = "Helvetica-Bold" | |
} | |
enum FontSize { | |
case standard(StandardSize) | |
case custom(Double) | |
var value: Double { | |
switch self { | |
case .standard(let size): | |
return size.rawValue | |
case .custom(let customSize): | |
return customSize | |
} | |
} | |
} | |
enum StandardSize: Double { | |
case h1 = 20.0 | |
case h2 = 18.0 | |
case h3 = 16.0 | |
case h4 = 14.0 | |
case h5 = 12.0 | |
case h6 = 10.0 | |
} | |
var type: FontType | |
var size: FontSize | |
init(_ type: FontType, size: FontSize) { | |
self.type = type | |
self.size = size | |
} | |
} | |
extension Font { | |
var instance: UIFont { | |
var instanceFont: UIFont! | |
switch type { | |
case .custom(let fontName): | |
guard let font = UIFont(name: fontName, size: CGFloat(size.value)) else { | |
fatalError("\(fontName) font is not installed, make sure it is added in Info.plist and logged with Utility.logAllAvailableFonts()") | |
} | |
instanceFont = font | |
case .installed(let fontName): | |
guard let font = UIFont(name: fontName.rawValue, size: CGFloat(size.value)) else { | |
fatalError("\(fontName.rawValue) font is not installed, make sure it is added in Info.plist and logged with Utility.logAllAvailableFonts()") | |
} | |
instanceFont = font | |
case .system: | |
instanceFont = UIFont.systemFont(ofSize: CGFloat(size.value)) | |
case .systemBold: | |
instanceFont = UIFont.boldSystemFont(ofSize: CGFloat(size.value)) | |
case .systemItatic: | |
instanceFont = UIFont.italicSystemFont(ofSize: CGFloat(size.value)) | |
case .systemWeighted(let weight): | |
instanceFont = UIFont.systemFont(ofSize: CGFloat(size.value), | |
weight: UIFont.Weight(rawValue: CGFloat(weight))) | |
case .monoSpacedDigit(let size, let weight): | |
instanceFont = UIFont.monospacedDigitSystemFont(ofSize: CGFloat(size), | |
weight: UIFont.Weight(rawValue: CGFloat(weight))) | |
} | |
return instanceFont | |
} | |
} | |
// Usage... | |
let font: UIFont = Font.init(.installed(.HelveticaRegular), size: .standard(.h1)).instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment