Skip to content

Instantly share code, notes, and snippets.

@Viveron
Last active July 23, 2018 13:54
Show Gist options
  • Select an option

  • Save Viveron/b170ec26f647274e7e33233ff5ea9b50 to your computer and use it in GitHub Desktop.

Select an option

Save Viveron/b170ec26f647274e7e33233ff5ea9b50 to your computer and use it in GitHub Desktop.
Application's fonts protocol for easy and convenient fonts makeup use
import Foundation
enum Roboto: String, FontFamily {
case thin = "Roboto-Thin"
case bold = "Roboto-Bold"
case light = "Roboto-Light"
case medium = "Roboto-Medium"
case regular = "Roboto-Regular"
}
import Foundation
// MARK: - Application's fonts protocol
protocol FontFamily {
var fontName: String { get }
}
// MARK: - FontFamily Protocol default implementation
extension FontFamily where Self: RawRepresentable, Self.RawValue == String {
var fontName: String {
return rawValue
}
}
// MARK: - UIFont extension for application's UI fonts
extension UIFont {
// Common font sizes in application
enum Size: CGFloat {
case small = 10
case lite = 12
case medium = 14
case normal = 16
case big = 20
case extra = 24
}
convenience init?(_ fontFamily: FontFamily, size: UIFont.Size) {
self.init(name: fontFamily.fontName, size: size.rawValue)
}
convenience init?(_ fontFamily: FontFamily, size: CGFloat) {
self.init(name: fontFamily.fontName, size: size)
}
}
@Viveron

Viveron commented Jul 23, 2018

Copy link
Copy Markdown
Author

Example:

let _ = UIFont(Roboto.bold, size: .normal)
let _ = UIFont(Roboto.bold, size: 56.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment