Last active
June 14, 2017 21:18
-
-
Save Liquidsoul/c094646b3a7975a6397f707c49a1304d to your computer and use it in GitHub Desktop.
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
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen | |
{% if families %} | |
#if os(OSX) | |
import AppKit.NSFont | |
typealias Font = NSFont | |
#elseif os(iOS) || os(tvOS) || os(watchOS) | |
import UIKit.UIFont | |
typealias Font = UIFont | |
#endif | |
// swiftlint:disable file_length | |
protocol FontConvertible { | |
var name: String { get } | |
var family: String { get } | |
var path: String { get } | |
func font(size: CGFloat) -> Font | |
} | |
extension FontConvertible { | |
func font(size: CGFloat) -> Font { | |
return Font(font: self, size: size)! | |
} | |
func register() { | |
let bundle = Bundle(for: BundleToken.self) | |
guard let url = bundle.url(forResource: path, withExtension: nil) else { | |
return | |
} | |
var errorRef: Unmanaged<CFError>? | |
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef) | |
} | |
} | |
extension Font { | |
convenience init!(font: FontConvertible, size: CGFloat) { | |
#if os(iOS) || os(tvOS) || os(watchOS) | |
if UIFont.fontNames(forFamilyName: font.family).isEmpty { | |
font.register() | |
} | |
#elseif os(OSX) | |
if NSFontManager.shared().availableMembers(ofFontFamily: font.family) == nil { | |
font.register() | |
} | |
#endif | |
self.init(name: font.name, size: size) | |
} | |
} | |
// swiftlint:disable identifier_name line_length type_body_length | |
enum {{param.enumName|default:"FontFamily"}} { | |
{% for family in families %} | |
enum {{family.name|swiftIdentifier|snakeToCamelCase:"true"|escapeReservedKeywords}}: FontConvertible { | |
{% for font in family.fonts %} | |
case {{font.style|swiftIdentifier|snakeToCamelCase:"true"|lowerFirstWord|escapeReservedKeywords}} | |
{% endfor %} | |
var name: String { | |
switch self { | |
{% for font in family.fonts %} | |
case .{{font.style|swiftIdentifier|snakeToCamelCase:"true"|lowerFirstWord|escapeReservedKeywords}}: return "{{font.name}}" | |
{% endfor %} | |
} | |
} | |
var family: String { | |
return "{{family.name}}" | |
} | |
var path: String { | |
switch self { | |
{% for font in family.fonts %} | |
case .{{font.style|swiftIdentifier|snakeToCamelCase:"true"|lowerFirstWord|escapeReservedKeywords}}: return "{{font.path}}" | |
{% endfor %} | |
} | |
} | |
{% map family.fonts into fontCases using font %}.{{font.style|swiftIdentifier|snakeToCamelCase:"true"|lowerFirstWord|escapeReservedKeywords}}{% endmap %} | |
static let all: [{{family.name|swiftIdentifier|snakeToCamelCase:"true"|escapeReservedKeywords}}] = [{{ fontCases|join }}] | |
} | |
{% endfor %} | |
} | |
// swiftlint:enable identifier_name line_length type_body_length | |
private final class BundleToken {} | |
{% else %} | |
// No fonts found | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment