Commit f714cc3a by Ramon Vicente

Fix load of custom font behind a custom bundle

parent 450ccf24
...@@ -58,4 +58,47 @@ public struct MaterialFont : MaterialFontType { ...@@ -58,4 +58,47 @@ public struct MaterialFont : MaterialFontType {
public static func italicSystemFontWithSize(size: CGFloat) -> UIFont { public static func italicSystemFontWithSize(size: CGFloat) -> UIFont {
return UIFont.italicSystemFontOfSize(size) 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 { ...@@ -48,7 +48,8 @@ public struct RobotoFont : MaterialFontType {
/** /**
:name: thinWithSize :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) { if let f = UIFont(name: "Roboto-Thin", size: size) {
return f return f
} }
...@@ -65,7 +66,8 @@ public struct RobotoFont : MaterialFontType { ...@@ -65,7 +66,8 @@ public struct RobotoFont : MaterialFontType {
/** /**
:name: lightWithSize :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) { if let f = UIFont(name: "Roboto-Light", size: size) {
return f return f
} }
...@@ -82,7 +84,8 @@ public struct RobotoFont : MaterialFontType { ...@@ -82,7 +84,8 @@ public struct RobotoFont : MaterialFontType {
/** /**
:name: regularWithSize :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) { if let f = UIFont(name: "Roboto-Regular", size: size) {
return f return f
} }
...@@ -92,7 +95,8 @@ public struct RobotoFont : MaterialFontType { ...@@ -92,7 +95,8 @@ public struct RobotoFont : MaterialFontType {
/** /**
:name: mediumWithSize :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) { if let f = UIFont(name: "Roboto-Medium", size: size) {
return f return f
} }
...@@ -117,6 +121,7 @@ public struct RobotoFont : MaterialFontType { ...@@ -117,6 +121,7 @@ public struct RobotoFont : MaterialFontType {
:name: boldWithSize :name: boldWithSize
*/ */
public static func boldWithSize(size: CGFloat) -> UIFont { public static func boldWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Bold")
if let f = UIFont(name: "Roboto-Bold", size: size) { if let f = UIFont(name: "Roboto-Bold", size: size) {
return f 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