Last active
July 20, 2017 03:04
-
-
Save justinmstuart/4cbe020c78d52012dfe8 to your computer and use it in GitHub Desktop.
UIFont Extension for Custom Dynamic Font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why Line 9 if we have same type of assignment in Line 22 ?