Skip to content
This repository was archived by the owner on Sep 6, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/fonts/swift4.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You can customize some elements of this template by overriding the following par
| -------------- | ------------- | ----------- |
| `enumName` | `FontFamily` | Allows you to change the name of the generated `enum` containing all font families. |
| `preservePath` | N/A | Setting this parameter will disable the basename filter applied to all font paths. Use this if you added your font folder as a "folder reference" in your Xcode project, making that folder hierarchy preserved once copied in the build app bundle. The path will be relative to the folder you provided to SwiftGen. |
| `accessModifier` | N/A | Allows you to add an access modifier to the types and functions generated by this template. Use this if need to access the generated code from another framework. |

## Generated Code

Expand Down
25 changes: 13 additions & 12 deletions templates/fonts/swift4.stencil
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/SwiftGen/SwiftGen
{% set accessModifier %}{% if param.accessModifier %}{{param.accessModifier}} {% endif %}{% endset %}

{% if families %}
#if os(OSX)
import AppKit.NSFont
typealias Font = NSFont
{{accessModifier}}typealias Font = NSFont
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIFont
typealias Font = UIFont
{{accessModifier}}typealias Font = UIFont
#endif

// swiftlint:disable file_length

struct FontConvertible {
{{accessModifier}}struct FontConvertible {
let name: String
let family: String
let path: String
{{accessModifier}}let family: String
{{accessModifier}}let path: String

func font(size: CGFloat) -> Font! {
{{accessModifier}}func font(size: CGFloat) -> Font! {
return Font(font: self, size: size)
}

func register() {
{{accessModifier}}func register() {
guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
Expand All @@ -32,8 +33,8 @@ struct FontConvertible {
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
{{accessModifier}}extension Font {
{{accessModifier}}convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
Expand All @@ -56,11 +57,11 @@ extension Font {
{{path|basename}}
{% endif %}
{% endfilter %}{% endmacro %}
enum {{param.enumName|default:"FontFamily"}} {
{{accessModifier}}enum {{param.enumName|default:"FontFamily"}} {
{% for family in families %}
enum {{family.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{{accessModifier}}enum {{family.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% for font in family.fonts %}
static let {{font.style|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = FontConvertible(name: "{{font.name}}", family: "{{family.name}}", path: "{% call transformPath font.path %}")
{{accessModifier}}static let {{font.style|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = FontConvertible(name: "{{font.name}}", family: "{{family.name}}", path: "{% call transformPath font.path %}")
{% endfor %}
}
{% endfor %}
Expand Down