Skip to content

Instantly share code, notes, and snippets.

@ryanrey
Created May 8, 2017 03:41
Show Gist options
  • Save ryanrey/e39c2d5cd9c9d10ce66443138ec40ab9 to your computer and use it in GitHub Desktop.
Save ryanrey/e39c2d5cd9c9d10ce66443138ec40ab9 to your computer and use it in GitHub Desktop.
UIFont Extension
typealias Font = UIFont
public extension Font {
convenience init(_ family: FontFamily = .system, size: CGFloat = 17.0, weight: FontWeight = FontWeight.regular) {
let descriptor: UIFontDescriptor
if family == .system {
descriptor = UIFont.systemFont(ofSize: size, weight: weight.value).fontDescriptor
} else {
var attributes: [String: AnyObject] = [:]
attributes[UIFontDescriptorNameAttribute] = family.name as AnyObject
attributes[UIFontWeightTrait] = weight.value as AnyObject
descriptor = UIFontDescriptor(fontAttributes: attributes)
}
self.init(descriptor: descriptor, size: size)
}
}
public enum FontWeight {
case ultraLight
case thin
case light
case regular
case medium
case semibold
case bold
case heavy
case black
var value: CGFloat {
switch self {
case .ultraLight: return UIFontWeightUltraLight
case .thin: return UIFontWeightThin
case .light: return UIFontWeightLight
case .regular: return UIFontWeightRegular
case .medium: return UIFontWeightMedium
case .semibold: return UIFontWeightSemibold
case .bold: return UIFontWeightBold
case .heavy: return UIFontWeightHeavy
case .black: return UIFontWeightBlack
}
}
}
public enum FontFamily: String {
case academyEngravedLET = "AcademyEngravedLET"
case alNile = "AlNile"
case americanTypewriter = "AmericanTypewriter"
case appleColorEmoji = "AppleColorEmoji"
case appleSDGothicNeo = "AppleSDGothicNeo"
case arial = "Arial"
case arialHebrew = "ArialHebrew"
case arialRoundedMTBold = "ArialRoundedMTBold"
case avenir = "Avenir"
case avenirNext = "AvenirNext"
case avenirNextCondensed = "AvenirNextCondensed"
case banglaSangamMN = "BanglaSangamMN"
case baskerville = "Baskerville"
case bodoni72 = "Bodoni72"
case bodoni72Oldstyle = "Bodoni72Oldstyle"
case bodoni72Smallcaps = "Bodoni72Smallcaps"
case bodoniOrnaments = "BodoniOrnaments"
case bradleyHand = "BradleyHand"
case chalkboardSE = "ChalkboardSE"
case chalkduster = "Chalkduster"
case cochin = "Cochin"
case copperplate = "Copperplate"
case courier = "Courier"
case courierNew = "CourierNew"
case damascus = "Damascus"
case devanagariSangamMN = "DevanagariSangamMN"
case didot = "Didot"
case euphemiaUCAS = "EuphemiaUCAS"
case farah = "Farah"
case futura = "Futura"
case geezaPro = "GeezaPro"
case georgia = "Georgia"
case gillSans = "GillSans"
case gujaratiSangamMN = "GujaratiSangamMN"
case gurmukhiMN = "GurmukhiMN"
case heitiSC = "HeitiSC"
case heitiTC = "HeitiTC"
case helvetica = "Helvetica"
case helveticaNeue = "HelveticaNeue"
case hiraginoMinchoProN = "HiraginoMinchoProN"
case hiraginoSans = "HiraginoSans"
case hoeflerText = "HoeflerText"
case kailasa = "Kailasa"
case kannadaSangamMN = "KannadaSangamMN"
case khmerSangamMN = "KhmerSangamMN"
case kohinoorBangla = "KohinoorBangla"
case kohinoorDevanagari = "KohinoorDevanagari"
case kohinoorTelugu = "KohinoorTelugu"
case laoSangamMN = "LaoSangamMN"
case malayalamSangamMN = "MalayalamSangamMN"
case markerFelt = "MarkerFelt"
case menlo = "Menlo"
case mishafi = "Mishafi"
case myanmarSangamMN = "MyanmarSangamMN"
case noteworthy = "Noteworthy"
case optima = "Optima"
case oriyaSangamMN = "OriyaSangamMN"
case palatino = "Palatino"
case papyrus = "Papyrus"
case partyLET = "PartyLET"
case pingFangHK = "PingFangHK"
case pingFangSC = "PingFangSC"
case pingFangTC = "PingFangTC"
case savoyeLET = "SavoyeLET"
case sinhalaSangamMN = "SinhalaSangamMN"
case snellRoundhand = "SnellRoundhand"
case symbol = "Symbol"
case system = "SanFransisco"
case tamilSangamMN = "TamilSangamMN"
case teluguSangamMN = "TeluguSangamMN"
case thonburi = "Thonburi"
case timesNewRoman = "TimesNewRoman"
case trebuchetMS = "TrebuchetMS"
case verdana = "Verdana"
case zapfDingbats = "ZapfDingbats"
case zapfino = "Zapfino"
var name: String {
return self.rawValue
}
var validWeights: [FontWeight] {
let fontWeights: [FontWeight]
switch self {
case .academyEngravedLET: fontWeights = [.regular]
case .alNile: fontWeights = [.regular, .bold]
case .americanTypewriter: fontWeights = [.regular, .light, .semibold, .bold]
case .appleColorEmoji: fontWeights = [.regular]
case .appleSDGothicNeo: fontWeights = [.regular, .bold, .ultraLight, .thin, .light]
case .arial: fontWeights = [.regular]
case .arialHebrew: fontWeights = [.regular, .bold, .light]
case .arialRoundedMTBold: fontWeights = [.regular]
case .avenir: fontWeights = [.regular, .light, .black, .heavy]
case .avenirNext: fontWeights = [.regular, .ultraLight, .bold, .heavy]
case .avenirNextCondensed: fontWeights = [.regular, .heavy, .ultraLight, .bold]
case .banglaSangamMN: fontWeights = [.regular]
case .baskerville: fontWeights = [.regular, .bold]
case .bodoni72: fontWeights = [.regular, .bold]
case .bodoni72Oldstyle: fontWeights = [.regular, .bold]
case .bodoni72Smallcaps: fontWeights = [.regular]
case .bodoniOrnaments: fontWeights = [.regular]
case .bradleyHand: fontWeights = [.regular, .bold]
case .chalkboardSE: fontWeights = [.regular, .bold, .light]
case .chalkduster: fontWeights = [.regular]
case .cochin: fontWeights = [.regular, .bold]
case .copperplate: fontWeights = [.regular, .light, .bold]
case .courier: fontWeights = [.regular, .bold]
case .courierNew: fontWeights = [.regular]
case .damascus: fontWeights = [.regular]
case .devanagariSangamMN: fontWeights = [.regular, .bold]
case .didot: fontWeights = [.regular, .bold]
case .euphemiaUCAS: fontWeights = [.regular, .bold]
case .farah: fontWeights = [.regular]
case .futura: fontWeights = [.regular, .bold]
case .geezaPro: fontWeights = [.regular, .bold]
case .georgia: fontWeights = [.regular, .bold]
case .gillSans: fontWeights = [.regular, .bold, .light]
case .gujaratiSangamMN: fontWeights = [.regular, .bold]
case .gurmukhiMN: fontWeights = [.regular, .bold]
case .heitiSC: fontWeights = [.regular]
case .heitiTC: fontWeights = [.regular]
case .helvetica: fontWeights = [.regular, .bold, .light]
case .helveticaNeue: fontWeights = [.regular, .bold, .ultraLight, .light, .thin]
case .hiraginoMinchoProN: fontWeights = [.regular]
case .hiraginoSans: fontWeights = [.regular]
case .hoeflerText: fontWeights = [.regular, .black]
case .kailasa: fontWeights = [.regular, .bold]
case .kannadaSangamMN: fontWeights = [.regular, .bold]
case .khmerSangamMN: fontWeights = [.regular]
case .kohinoorBangla: fontWeights = [.regular, .semibold, .light]
case .kohinoorDevanagari: fontWeights = [.regular, .light, .semibold]
case .kohinoorTelugu: fontWeights = [.regular, .light]
case .laoSangamMN: fontWeights = [.regular]
case .malayalamSangamMN: fontWeights = [.regular, .bold]
case .markerFelt: fontWeights = [.regular, .thin]
case .menlo: fontWeights = [.regular, .bold]
case .mishafi: fontWeights = [.regular]
case .myanmarSangamMN: fontWeights = [.regular, .bold]
case .noteworthy: fontWeights = [.regular, .light, .bold]
case .optima: fontWeights = [.regular, .bold]
case .oriyaSangamMN: fontWeights = [.regular, .bold]
case .palatino: fontWeights = [.regular, .bold]
case .papyrus: fontWeights = [.regular]
case .partyLET: fontWeights = [.regular]
case .pingFangHK: fontWeights = [.regular, .semibold, .thin, .light]
case .pingFangSC: fontWeights = [.regular, .semibold, .thin, .light]
case .pingFangTC: fontWeights = [.regular, .light, .semibold, .thin]
case .savoyeLET: fontWeights = [.regular]
case .sinhalaSangamMN: fontWeights = [.regular, .bold]
case .snellRoundhand: fontWeights = [.regular, .bold, .black]
case .symbol: fontWeights = [.regular]
case .system: fontWeights = [.regular, .bold, .ultraLight, .light, .thin]
case .tamilSangamMN: fontWeights = [.regular, .bold]
case .teluguSangamMN: fontWeights = [.regular]
case .thonburi: fontWeights = [.regular, .bold, .light]
case .timesNewRoman: fontWeights = [.regular]
case .trebuchetMS: fontWeights = [.regular, .bold]
case .verdana: fontWeights = [.regular, .bold]
case .zapfDingbats: fontWeights = [.regular]
case .zapfino: fontWeights = [.regular]
}
return fontWeights
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment