Simplified Version? No longer requires UIKit, only for SwiftUI? Must test

This commit is contained in:
Robert McGovern 2021-07-06 01:45:25 +01:00
parent e049a2cb60
commit 3538f491f8
4 changed files with 40 additions and 29 deletions

View File

@ -23,7 +23,7 @@ let package = Package(
name: "CustomFontsPackage",
dependencies: [],
exclude: ["Images"],
resources: [.process("Fonts")]
resources: [.process("Resources")]
),
.testTarget(
name: "CustomFontsPackageTests",

View File

@ -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<CFError>?
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<CFError>?
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<CFError>?
// 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!"
}