Skip to content

Instantly share code, notes, and snippets.

@mspvirajpatel
Created August 2, 2017 10:43
Show Gist options
  • Save mspvirajpatel/ca60fcf70471ef4502d04a03a088a4fa to your computer and use it in GitHub Desktop.
Save mspvirajpatel/ca60fcf70471ef4502d04a03a088a4fa to your computer and use it in GitHub Desktop.
Font Extension For Swift
import Foundation
import UIKit
struct SFUIDisplayFontType : OptionSetType
{
let rawValue: Int
init(rawValue: Int) { self.rawValue = rawValue }
static internal let None = SFUIDisplayFontType(rawValue: 0)
static internal let Bold = SFUIDisplayFontType(rawValue: 1)
static internal let Semibold = SFUIDisplayFontType(rawValue: 2)
static internal let Heavy = SFUIDisplayFontType(rawValue: 3)
static internal let Black = SFUIDisplayFontType(rawValue: 4)
static internal let Medium = SFUIDisplayFontType(rawValue: 5)
static internal let Regular = SFUIDisplayFontType(rawValue: 6)
static internal let Thin = SFUIDisplayFontType(rawValue: 7)
static internal let Light = SFUIDisplayFontType(rawValue: 8)
static internal let Ultralight = SFUIDisplayFontType(rawValue: 9)
}
extension UIFont
{
class func SFUIDisplayFont(type: SFUIDisplayFontType, size: CGFloat)-> UIFont
{
var post:String
switch (type)
{
switch (type)
{
case SFUIDisplayFontType.Bold:
post = "Bold"
break
case SFUIDisplayFontType.Semibold:
post = "Semibold"
break
case SFUIDisplayFontType.Heavy:
post = "Heavy"
break
case SFUIDisplayFontType.Black:
post = "Black"
break
case SFUIDisplayFontType.Medium:
post = "Medium"
break
case SFUIDisplayFontType.Regular:
post = "Regular"
break
case SFUIDisplayFontType.Thin:
post = "Thin"
break
case SFUIDisplayFontType.Light:
post = "Light"
break
case SFUIDisplayFontType.Ultralight:
post = "Ultralight"
break
default:
post = "Regular"
break
}
}
let fontName:String = "SFUIDisplay-"+post
return UIFont(name: fontName, size: size)!
}
class func SFUIDisplayFontBoldWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Bold", size: size)!
}
class func SFUIDisplayFontSemiBoldWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Semibold", size: size)!
}
class func SFUIDisplayFontHeavyWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Heavy", size: size)!
}
class func SFUIDisplayFontBlackWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Black", size: size)!
}
class func SFUIDisplayFontMediumWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Medium", size: size)!
}
class func SFUIDisplayFontRegularWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Regular", size: size)!
}
class func SFUIDisplayFontThinWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Thin", size: size)!
}
class func SFUIDisplayFontLightWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Light", size: size)!
}
class func SFUIDisplayFontUltraLightWithSize(size: CGFloat) -> UIFont
{
return UIFont(name: "SFUIDisplay-Ultralight", size: size)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment