├── Font.swift └── README.md /Font.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Font.swift 3 | // SaferFonts 4 | // 5 | // Created by Renato Rodrigues on 24/08/2015. 6 | // Copyright © 2015 SaferFonts. All rights reserved. 7 | // 8 | 9 | protocol Font { 10 | func name() -> String 11 | } 12 | 13 | extension UIFont { 14 | convenience init(font: Font, size: CGFloat) { 15 | self.init(name: font.name(), size: size)! 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Why "Safer"? 2 | 3 | Let's imagine we want to set a `UILabel`'s font: 4 | 5 | ```swift 6 | myLabel.font = UIFont(name: "ArialMT", size: 14)! 7 | ``` 8 | 9 | This approach is error prone and inherently invites for duplication. 10 | 11 | 1. We could easily misspell `"ArialMT"` and crash the app. Since `UIFont`'s `name:size:` returns an optional, for practical reasons we would just force unwrap it. (in this particular case it doesn't make sense to use optional binding) 12 | 2. If we need to use the same font in some other place, we might be tempted to just copy/paste it. 13 | 14 | SaferFonts, in this case, just bundles the font in a convenient `enum`: 15 | 16 | ```swift 17 | myLabel.font = UIFont(name: ArialMT.Default.rawValue, size: 14)! 18 | ``` 19 | 20 | As you might have noticed by now, SaferFont is not a 3rd party library that you can just get using Carthage or Cocopods. It would be a significant burden to use a library with dozens of fonts, when you just need one or two. So instead, this can be viewed as an approach and a database of fonts that you can use. 21 | 22 | ### The Font Protocol 23 | 24 | The [Font protocol](https://github.com/RuiAAPeres/SaferFonts/blob/master/Font.swift) (suggested by [renatorodrigues](https://github.com/renatorodrigues)), can also be used to make this enum easier to consume. For example with the font `FooBar` : 25 | 26 | ```swift 27 | let fooBarFont = UIFont(font: FooBar.Bold, size: 15) 28 | ``` 29 | 30 | In order to make the enum compliant: 31 | 32 | ```swift 33 | private enum FooBar: String, Font { 34 | case Bold = "FooBar-Bold" 35 | 36 | func name() -> String { 37 | return self.rawValue 38 | } 39 | } 40 | ``` 41 | 42 | ### ArialMT 43 | 44 | ```swift 45 | enum ArialMT: String { 46 | case Regular = "ArialMT" 47 | case Bold = "Arial-BoldMT" 48 | case Italic = "Arial-ItalicMT" 49 | case BoldItalic = "Arial-BoldItalicMT" 50 | } 51 | ``` 52 | ### HelveticaNeue 53 | 54 | ```swift 55 | enum HelveticaNeue: String { 56 | case Regular = "HelveticaNeue" 57 | case Italic = "HelveticaNeue-Italic" 58 | case Bold = "HelveticaNeue-Bold" 59 | case BoldItalic = "HelveticaNeue-BoldItalic" 60 | case Medium = "HelveticaNeue-Medium" 61 | case MediumItalic = "HelveticaNeue-MediumItalic" 62 | case Light = "HelveticaNeue-Light" 63 | case LightItalic = "HelveticaNeue-LightItalic" 64 | case UltraLight = "HelveticaNeue-UltraLight" 65 | case UltraLightItalic = "HelveticaNeue-UltraLightItalic" 66 | case Thin = "HelveticaNeue-Thin" 67 | case ThinItalic = "HelveticaNeue-ThinItalic" 68 | case CondensedBlack = "HelveticaNeue-CondensedBlack" 69 | case CondensedBold = "HelveticaNeue-CondensedBold" 70 | } 71 | ``` 72 | --------------------------------------------------------------------------------