Skip to content

Instantly share code, notes, and snippets.

@RodolfoAntonici
Last active December 7, 2015 16:09
Show Gist options
  • Save RodolfoAntonici/554cf29b2c774fb0c8bb to your computer and use it in GitHub Desktop.
Save RodolfoAntonici/554cf29b2c774fb0c8bb to your computer and use it in GitHub Desktop.
//
// EasyFont.swift
//
// Created by Rodolfo Antonici on 7/12/15.
// Copyright © 2015 Rodolfo Antonici. All rights reserved.
//
/* Basicaly a font class that wraps the fonts weights and translate it to an Enum with a printable value and complements it with and San Francisco font class*/
/* To download the SF fonts go to: https://developer.apple.com/fonts/ */
import UIKit
// Beware that most fonts will _not_ have variants available in all these weights!
public enum FontWeight: CustomStringConvertible {
case UltraLight
case Thin
case Light
case Regular
case Medium
case SemiBold
case Bold
case Heavy
case Black
static func fontWeightFromFloat(weight: CGFloat) -> FontWeight {
switch weight {
case UIFontWeightUltraLight:
return FontWeight.UltraLight
case UIFontWeightThin:
return FontWeight.Thin
case UIFontWeightLight:
return FontWeight.Light
case UIFontWeightMedium:
return FontWeight.Medium
case UIFontWeightSemibold:
return FontWeight.SemiBold
case UIFontWeightBold:
return FontWeight.Bold
case UIFontWeightHeavy:
return FontWeight.Heavy
case UIFontWeightBlack:
return FontWeight.Black
default:
return FontWeight.Regular
}
}
public var description: String {
var weightString = "Regular"
switch self {
case UltraLight:
weightString = "Ultralight"
case Thin:
weightString = "Thin"
case Light:
weightString = "Light"
case Regular:
weightString = "Regular"
case Medium:
weightString = "Medium"
case SemiBold:
weightString = "Semibold"
case Bold:
weightString = "Bold"
case Heavy:
weightString = "Heavy"
case Black:
weightString = "Black"
}
return weightString
}
}
public extension UIFont {
public class func sanFranciscoDisplayFontOfSize(fontSize: CGFloat, weight: CGFloat) -> UIFont? {
return UIFont(name: "SF-UI-Display-\(FontWeight.fontWeightFromFloat(weight).description)", size: fontSize)
}
public class func sanFranciscoTextFontOfSize(fontSize: CGFloat, weight: CGFloat, italic: Bool) -> UIFont? {
let italicString = italic ? "Italic" : ""
return UIFont(name: "SF-UI-Text-\(FontWeight.fontWeightFromFloat(weight).description)\(italicString)", size: fontSize)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment