我在框架中添加了自定义字体。我遵循了所有的步骤,但它不起作用。
我可以在Interface Builder中设置字体,但当我构建项目时,它不会在模拟器/设备上显示此字体。
发布于 2015-10-01 00:11:19
以下是我对John的回答,展示了如果您有很多字体时如何调用该函数
import Foundation
extension UIFont {
@nonobjc static var loadAllFontsDO: dispatch_once_t = 0
class func initialsAvatarFont() -> UIFont {
loadAllFonts()
if let retval = UIFont(name: "MyFontName", size: kInitialsAvatarFontSize) {
return retval;
} else {
return UIFont.systemFontOfSize(kInitialsAvatarFontSize)
}
}
class func loadAllFonts() {
dispatch_once(&loadAllFontsDO) { () -> Void in
registerFontWithFilenameString("thefontfilename.ttf", bundleIdentifierString: "nameOfResourceBundleAlongsideTheFrameworkBundle")
// Add more font files here as required
}
}
static func registerFontWithFilenameString(filenameString: String, bundleIdentifierString: String) {
let frameworkBundle = NSBundle(forClass: AnyClassInYourFramework.self)
let resourceBundleURL = frameworkBundle.URLForResource(bundleIdentifierString, withExtension: "bundle")
if let bundle = NSBundle(URL: resourceBundleURL!) {
let pathForResourceString = bundle.pathForResource(filenameString, ofType: nil)
let fontData = NSData(contentsOfFile: pathForResourceString!)
let dataProvider = CGDataProviderCreateWithCFData(fontData)
let fontRef = CGFontCreateWithDataProvider(dataProvider)
var errorRef: Unmanaged<CFError>? = nil
if (CTFontManagerRegisterGraphicsFont(fontRef!, &errorRef) == false) {
NSLog("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
}
}
else {
NSLog("Failed to register font - bundle identifier invalid.")
}
}
}
发布于 2015-05-28 23:41:09
通过在框架中实现+load
方法,您可以从动态框架中加载和使用捆绑的自定义字体。
在load
方法中,找到捆绑包中的字体,然后注册它们。这使得它们对应用程序可用,而不必在主项目中指定它们。
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Dynamically load bundled custom fonts
[self bible_loadFontWithName:kBIBLECustomFontBoldName];
[self bible_loadFontWithName:kBIBLECustomFontBoldItalicName];
[self bible_loadFontWithName:kBIBLECustomFontItalicName];
[self bible_loadFontWithName:kBIBLECustomFontRegularName];
});
}
+ (void)bible_loadFontWithName:(NSString *)fontName
{
NSString *fontPath = [[NSBundle bundleForClass:[BIBLE class]] pathForResource:fontName ofType:@"otf"];
NSData *fontData = [NSData dataWithContentsOfFile:fontPath];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
if (provider)
{
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (font)
{
CFErrorRef error = NULL;
if (CTFontManagerRegisterGraphicsFont(font, &error) == NO)
{
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
}
CFRelease(provider);
}
}
发布于 2020-08-17 20:20:44
以下解决方案允许您自动加载具有特定扩展名的所有字体:
static func registerFonts() {
let fonts = Bundle(for:
OrientationMonitor.self).urls(forResourcesWithExtension: "ttf", subdirectory: nil)
fonts?.forEach({ url in
CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
})
}
请确保将OrientationMonitor
替换为框架中现有的类。
https://stackoverflow.com/questions/30507905
复制相似问题