Simplified Version? No longer requires UIKit, only for SwiftUI? Must test
This commit is contained in:
parent
e049a2cb60
commit
3538f491f8
|
@ -23,7 +23,7 @@ let package = Package(
|
||||||
name: "CustomFontsPackage",
|
name: "CustomFontsPackage",
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
exclude: ["Images"],
|
exclude: ["Images"],
|
||||||
resources: [.process("Fonts")]
|
resources: [.process("Resources")]
|
||||||
),
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "CustomFontsPackageTests",
|
name: "CustomFontsPackageTests",
|
||||||
|
|
|
@ -1,38 +1,49 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
import UIKit
|
//import UIKit
|
||||||
|
|
||||||
public let fontBundle = Bundle.module
|
public let fontBundle = Bundle.module
|
||||||
|
|
||||||
public func registerFonts() {
|
//public func registerFonts() {
|
||||||
_ = UIFont.registerFont(bundle: .module, fontName: "BeautifulPeople-Regular", fontExtension: "ttf")
|
// _ = UIFont.registerFont(bundle: .module, fontName: "BeautifulPeople-Regular", fontExtension: "ttf")
|
||||||
_ = UIFont.registerFont(bundle: .module, fontName: "VeganStyle-Regular", fontExtension: "ttf")
|
// _ = UIFont.registerFont(bundle: .module, fontName: "VeganStyle-Regular", fontExtension: "ttf")
|
||||||
}
|
//}
|
||||||
|
|
||||||
extension UIFont {
|
func registerFont(_ name: String, fileExtension: String) {
|
||||||
static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) -> Bool {
|
guard let fontURL = Bundle.module.url(forResource: name, withExtension: fileExtension) else {
|
||||||
|
print("No font named \(name).\(fileExtension) was found in the module bundle")
|
||||||
guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension) else {
|
return
|
||||||
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>?
|
var error: Unmanaged<CFError>?
|
||||||
let success = CTFontManagerRegisterGraphicsFont(font, &error)
|
CTFontManagerRegisterFontsForURL(fontURL as CFURL, .process, &error)
|
||||||
guard success else {
|
print(error ?? "Successfully registered font: \(name)")
|
||||||
print("Error registering font: \(fontName). Maybe it was already registered.")
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
//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 {
|
struct CustomFontsPackage {
|
||||||
var text = "Hello, World!"
|
var text = "Hello, World!"
|
||||||
|
|
Loading…
Reference in New Issue