Skip to content

Commit

Permalink
Merge pull request #23 from tichise/feature/sample
Browse files Browse the repository at this point in the history
MaterialDesignFontをsingletonに変更して、registerFontを初期化時に1度だけ実行するようにした
  • Loading branch information
tichise committed Feb 27, 2021
2 parents 934ba13 + e411e37 commit f28b312
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion MaterialDesignSymbol.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MaterialDesignSymbol'
s.version = '2.6.8'
s.version = '2.6.9'
s.license = {
:type => "MIT",
:text => <<-LICENSE
Expand Down
23 changes: 17 additions & 6 deletions Sources/MaterialDesignSymbol/MaterialDesignFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ import UIKit
*/
public struct MaterialDesignFont {

private static func registerFont(name: String, fileExtension: String) {
static let shared = MaterialDesignFont()

/// 呼び出すアイコンファイル名
private let name = "material-design-icons"

private init() {
loadFont()
}

/// このメソッドはSPMの場合だけ使います。
public func loadFont() {
/// 呼び出すアイコンファイル名
registerFont(name: name, fileExtension: "ttf")
}

private func registerFont(name: String, fileExtension: String) {
#if SWIFT_PACKAGE
guard let fontURL = Bundle.module.url(forResource: name, withExtension: fileExtension) else {
print("No font named \(name).\(fileExtension) was found in the module bundle")
Expand All @@ -30,12 +45,8 @@ public struct MaterialDesignFont {
- parameter fontSize: フォントサイズ
- returns: UIFont
*/
public static func fontOfSize(_ fontSize: CGFloat) -> UIFont {
/// 呼び出すアイコンファイル名
let name = "material-design-icons"
public func fontOfSize(_ fontSize: CGFloat) -> UIFont {

registerFont(name: name, fileExtension: "ttf")

// アイコンを呼び出す
if UIFont.fontNames(forFamilyName: name).count == 0 {
do {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MaterialDesignSymbol/MaterialDesignSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MaterialDesignSymbol {
self.mutableTextFontAttributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
}

self.mutableTextFontAttributes[NSAttributedString.Key.font] = MaterialDesignFont.fontOfSize(size)
self.mutableTextFontAttributes[NSAttributedString.Key.font] = MaterialDesignFont.shared.fontOfSize(size)
}

public init(text: String, size: CGFloat) {
Expand All @@ -40,7 +40,7 @@ public class MaterialDesignSymbol {
self.mutableTextFontAttributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
}

self.mutableTextFontAttributes[NSAttributedString.Key.font] = MaterialDesignFont.fontOfSize(size)
self.mutableTextFontAttributes[NSAttributedString.Key.font] = MaterialDesignFont.shared.fontOfSize(size)
}

// MARK: - Method
Expand Down
2 changes: 1 addition & 1 deletion Sources/MaterialDesignSymbol/UILabel+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
extension UILabel {

public func set(icon: MaterialDesignIconEnum, fontSize: CGFloat) {
self.font = MaterialDesignFont.fontOfSize(fontSize)
self.font = MaterialDesignFont.shared.fontOfSize(fontSize)
self.text = icon.rawValue
}
}

0 comments on commit f28b312

Please sign in to comment.