Skip to content

Instantly share code, notes, and snippets.

@justinmstuart
Last active July 20, 2017 03:04
Show Gist options
  • Save justinmstuart/4cbe020c78d52012dfe8 to your computer and use it in GitHub Desktop.
Save justinmstuart/4cbe020c78d52012dfe8 to your computer and use it in GitHub Desktop.
UIFont Extension for Custom Dynamic Font
import UIKit
extension UIFont {
class func preferredCustomFontForTextStyle (textStyle: NSString) -> UIFont {
let fontsDictionary = NSDictionary (contentsOfFile: NSBundle.mainBundle().pathForResource("CustomFontNames", ofType: "plist")!)
let font = UIFontDescriptor.preferredFontDescriptorWithTextStyle(textStyle)
let fontSize: CGFloat = font.pointSize
if textStyle == UIFontTextStyleHeadline || textStyle == UIFontTextStyleSubheadline {
if let boldFontName = (fontsDictionary.valueForKey("Bold Font Name") as String?) {
return UIFont(name: boldFontName, size: fontSize)
}
else {
NSLog("Fonts.plist is missing a value for the “Bold Font” key?")
return UIFont.boldSystemFontOfSize(fontSize)
}
}
else {
if let regularFontName = (fontsDictionary.valueForKey("Regular Font Name") as String?) {
return UIFont(name: regularFontName, size: fontSize)
}
else {
NSLog("Fonts.plist is missing a value for the “Regular Font” key?")
return UIFont.systemFontOfSize(fontSize)
}
}
}
}
@evgeniyd
Copy link

evgeniyd commented Nov 4, 2014

Why Line 9 if we have same type of assignment in Line 22 ?

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