From 3538f491f80580de326d638e7bf51e4668219867 Mon Sep 17 00:00:00 2001 From: Robert McGovern Date: Tue, 6 Jul 2021 01:45:25 +0100 Subject: [PATCH] Simplified Version? No longer requires UIKit, only for SwiftUI? Must test --- Package.swift | 2 +- .../CustomFontsPackage.swift | 67 ++++++++++-------- .../BeautifulPeople-Regular.ttf | Bin .../VeganStyle-Regular.ttf | Bin 4 files changed, 40 insertions(+), 29 deletions(-) rename Sources/CustomFontsPackage/{Fonts => Resources}/BeautifulPeople-Regular.ttf (100%) rename Sources/CustomFontsPackage/{Fonts => Resources}/VeganStyle-Regular.ttf (100%) diff --git a/Package.swift b/Package.swift index d73de60..770a7f1 100644 --- a/Package.swift +++ b/Package.swift @@ -23,7 +23,7 @@ let package = Package( name: "CustomFontsPackage", dependencies: [], exclude: ["Images"], - resources: [.process("Fonts")] + resources: [.process("Resources")] ), .testTarget( name: "CustomFontsPackageTests", diff --git a/Sources/CustomFontsPackage/CustomFontsPackage.swift b/Sources/CustomFontsPackage/CustomFontsPackage.swift index 109c750..b5e55cf 100644 --- a/Sources/CustomFontsPackage/CustomFontsPackage.swift +++ b/Sources/CustomFontsPackage/CustomFontsPackage.swift @@ -1,39 +1,50 @@ import Foundation -import UIKit +//import UIKit public let fontBundle = Bundle.module -public func registerFonts() { - _ = UIFont.registerFont(bundle: .module, fontName: "BeautifulPeople-Regular", fontExtension: "ttf") - _ = UIFont.registerFont(bundle: .module, fontName: "VeganStyle-Regular", fontExtension: "ttf") -} +//public func registerFonts() { +// _ = UIFont.registerFont(bundle: .module, fontName: "BeautifulPeople-Regular", fontExtension: "ttf") +// _ = UIFont.registerFont(bundle: .module, fontName: "VeganStyle-Regular", fontExtension: "ttf") +//} -extension UIFont { - static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) -> Bool { - - guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension) else { - fatalError("Couldn't find font \(fontName)") - } - - guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL) else { - fatalError("Couldn't load data from the font \(fontName)") - } - - guard let font = CGFont(fontDataProvider) else { - fatalError("Couldn't create font from data") - } - - var error: Unmanaged? - let success = CTFontManagerRegisterGraphicsFont(font, &error) - guard success else { - print("Error registering font: \(fontName). Maybe it was already registered.") - return false - } - - return true +func registerFont(_ name: String, fileExtension: String) { + guard let fontURL = Bundle.module.url(forResource: name, withExtension: fileExtension) else { + print("No font named \(name).\(fileExtension) was found in the module bundle") + return } + + var error: Unmanaged? + CTFontManagerRegisterFontsForURL(fontURL as CFURL, .process, &error) + print(error ?? "Successfully registered font: \(name)") } +//extension UIFont { +// static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) -> Bool { +// +// guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension) else { +// fatalError("Couldn't find font \(fontName)") +// } +// +// guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL) else { +// fatalError("Couldn't load data from the font \(fontName)") +// } +// +// guard let font = CGFont(fontDataProvider) else { +// fatalError("Couldn't create font from data") +// } +// +// var error: Unmanaged? +// let success = CTFontManagerRegisterGraphicsFont(font, &error) +// guard success else { +// print("Error registering font: \(fontName). Maybe it was already registered.") +// return false +// } +// +// return true +// } +//} + struct CustomFontsPackage { var text = "Hello, World!" } diff --git a/Sources/CustomFontsPackage/Fonts/BeautifulPeople-Regular.ttf b/Sources/CustomFontsPackage/Resources/BeautifulPeople-Regular.ttf similarity index 100% rename from Sources/CustomFontsPackage/Fonts/BeautifulPeople-Regular.ttf rename to Sources/CustomFontsPackage/Resources/BeautifulPeople-Regular.ttf diff --git a/Sources/CustomFontsPackage/Fonts/VeganStyle-Regular.ttf b/Sources/CustomFontsPackage/Resources/VeganStyle-Regular.ttf similarity index 100% rename from Sources/CustomFontsPackage/Fonts/VeganStyle-Regular.ttf rename to Sources/CustomFontsPackage/Resources/VeganStyle-Regular.ttf