Commit 0235e38f by Daniel Dahan

added MaterialFontLoader to aid in loading packaged fonts with Material

parent bdeff5e3
......@@ -33,72 +33,58 @@ import UIKit
public protocol MaterialFontType {}
public struct MaterialFont : MaterialFontType {
/**
:name: pointSize
*/
/// Size of font.
public static let pointSize: CGFloat = 16
/**
:name: systemFontWithSize
*/
/// Retrieves the system font with a specified size.
public static func systemFontWithSize(size: CGFloat) -> UIFont {
return UIFont.systemFontOfSize(size)
}
/**
:name: boldSystemFontWithSize
*/
/// Retrieves the bold system font with a specified size.
public static func boldSystemFontWithSize(size: CGFloat) -> UIFont {
return UIFont.boldSystemFontOfSize(size)
}
/**
:name: italicSystemFontWithSize
*/
/// Retrieves the italic system font with a specified size.
public static func italicSystemFontWithSize(size: CGFloat) -> UIFont {
return UIFont.italicSystemFontOfSize(size)
}
public static func loadFontIfNeeded(fontName:String) {
FontLoader.loadFontIfNeeded(fontName)
/// Loads a font if it is needed.
public static func loadFontIfNeeded(fontName: String) {
MaterialFontLoader.loadFontIfNeeded(fontName)
}
}
/// Loads fonts packaged with Material.
private class MaterialFontLoader {
/// A Dictionary of the fonts already loaded.
static var loadedFonts: Dictionary<String, String> = Dictionary<String, String>()
private class FontLoader {
/// Loads a font specified if needed.
static func loadFontIfNeeded(fontName: String) {
let loadedFont: String? = MaterialFontLoader.loadedFonts[fontName]
static var loadedFonts:[String: String] = [:]
if nil == loadedFont && nil == UIFont(name: fontName, size: 1) {
MaterialFontLoader.loadedFonts[fontName] = fontName
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 = NSBundle(forClass: MaterialFontLoader.self)
let identifier: String? = bundle.bundleIdentifier
let fontURL: NSURL? = true == identifier?.hasPrefix("org.cocoapods") ? bundle.URLForResource(fontName, withExtension: "ttf", subdirectory: "io.cosmicmind.material.fonts.bundle") : bundle.URLForResource(fontName, withExtension: "ttf")
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)!
if let v: NSURL = fontURL {
let data: NSData = NSData(contentsOfURL: v)!
let provider: CGDataProvider = CGDataProviderCreateWithCFData(data)!
let font: CGFont = CGFontCreateWithDataProvider(provider)!
var error: Unmanaged<CFError>?
if !CTFontManagerRegisterGraphicsFont(font, &error) {
let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
let nsError: NSError = error!.takeUnretainedValue() as AnyObject as! NSError
NSException(name: NSInternalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
}
}
}
}
}
\ No newline at end of file
......@@ -31,23 +31,17 @@
import UIKit
public struct RobotoFont : MaterialFontType {
/**
:name: pointSize
*/
/// Size of font.
public static var pointSize: CGFloat {
return MaterialFont.pointSize
}
/**
:name: thin
*/
/// Thin font.
public static var thin: UIFont {
return thinWithSize(MaterialFont.pointSize)
}
/**
:name: thinWithSize
*/
/// Thin with size font.
public static func thinWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Thin")
if let f = UIFont(name: "Roboto-Thin", size: size) {
......@@ -56,16 +50,12 @@ public struct RobotoFont : MaterialFontType {
return MaterialFont.systemFontWithSize(size)
}
/**
:name: light
*/
/// Light font.
public static var light: UIFont {
return lightWithSize(MaterialFont.pointSize)
}
/**
:name: lightWithSize
*/
/// Light with size font.
public static func lightWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Light")
if let f = UIFont(name: "Roboto-Light", size: size) {
......@@ -74,16 +64,12 @@ public struct RobotoFont : MaterialFontType {
return MaterialFont.systemFontWithSize(size)
}
/**
:name: regular
*/
/// Regular font.
public static var regular: UIFont {
return regularWithSize(MaterialFont.pointSize)
}
/**
:name: regularWithSize
*/
/// Regular with size font.
public static func regularWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Regular")
if let f = UIFont(name: "Roboto-Regular", size: size) {
......@@ -92,9 +78,12 @@ public struct RobotoFont : MaterialFontType {
return MaterialFont.systemFontWithSize(size)
}
/**
:name: mediumWithSize
*/
/// Medium font.
public static var medium: UIFont {
return mediumWithSize(MaterialFont.pointSize)
}
/// Medium with size font.
public static func mediumWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Medium")
if let f = UIFont(name: "Roboto-Medium", size: size) {
......@@ -103,23 +92,12 @@ public struct RobotoFont : MaterialFontType {
return MaterialFont.boldSystemFontWithSize(size)
}
/**
:name: medium
*/
public static var medium: UIFont {
return mediumWithSize(MaterialFont.pointSize)
}
/**
:name: bold
*/
/// Bold font.
public static var bold: UIFont {
return boldWithSize(MaterialFont.pointSize)
}
/**
:name: boldWithSize
*/
/// Bold with size font.
public static func boldWithSize(size: CGFloat) -> UIFont {
MaterialFont.loadFontIfNeeded("Roboto-Bold")
if let f = UIFont(name: "Roboto-Bold", size: size) {
......
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