Commit bdeff5e3 by Daniel Dahan

Merge pull request #288 from ramonvic/material-fonts-not-loaded

This is great. I will merge and look more into it. Thank you!
parents 8525e33a 79da0914
......@@ -58,4 +58,47 @@ public struct MaterialFont : MaterialFontType {
public static func italicSystemFontWithSize(size: CGFloat) -> UIFont {
return UIFont.italicSystemFontOfSize(size)
}
public static func loadFontIfNeeded(fontName:String) {
FontLoader.loadFontIfNeeded(fontName)
}
}
private class FontLoader {
static var loadedFonts:[String: String] = [:]
static func loadFontIfNeeded(fontName:String) {
let loadedFont = FontLoader.loadedFonts[fontName]
if (loadedFont == nil && UIFont(name: fontName, size: 1) == nil) {
FontLoader.loadedFonts[fontName] = fontName
let bundle = NSBundle(forClass: FontLoader.self)
var fontURL:NSURL? = nil
let identifier = bundle.bundleIdentifier
if identifier?.hasPrefix("org.cocoapods") == true {
fontURL = bundle.URLForResource(fontName, withExtension: "ttf", subdirectory: "io.cosmicmind.material.fonts.bundle")
} else {
fontURL = bundle.URLForResource(fontName, withExtension: "ttf")
}
if fontURL != nil {
let data = NSData(contentsOfURL: fontURL!)!
let provider = CGDataProviderCreateWithCFData(data)
let font = CGFontCreateWithDataProvider(provider)!
var error: Unmanaged<CFError>?
if !CTFontManagerRegisterGraphicsFont(font, &error) {
let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
NSException(name: NSInternalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
}
}
}
}
}
\ No newline at end of file
......@@ -48,7 +48,8 @@ public struct RobotoFont : MaterialFontType {
/**
:name: thinWithSize
*/
public static func thinWithSize(size: CGFloat) -> UIFont {
public static func thinWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Thin")
if let f = UIFont(name: "Roboto-Thin", size: size) {
return f
}
......@@ -65,7 +66,8 @@ public struct RobotoFont : MaterialFontType {
/**
:name: lightWithSize
*/
public static func lightWithSize(size: CGFloat) -> UIFont {
public static func lightWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Light")
if let f = UIFont(name: "Roboto-Light", size: size) {
return f
}
......@@ -82,7 +84,8 @@ public struct RobotoFont : MaterialFontType {
/**
:name: regularWithSize
*/
public static func regularWithSize(size: CGFloat) -> UIFont {
public static func regularWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Regular")
if let f = UIFont(name: "Roboto-Regular", size: size) {
return f
}
......@@ -92,7 +95,8 @@ public struct RobotoFont : MaterialFontType {
/**
:name: mediumWithSize
*/
public static func mediumWithSize(size: CGFloat) -> UIFont {
public static func mediumWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Medium")
if let f = UIFont(name: "Roboto-Medium", size: size) {
return f
}
......@@ -117,6 +121,7 @@ public struct RobotoFont : MaterialFontType {
:name: boldWithSize
*/
public static func boldWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Bold")
if let f = UIFont(name: "Roboto-Bold", size: size) {
return f
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment