gpt4 book ai didi

iOS - 下载动态字体

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:15 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序动态下载字体,因为我经常想即时添加新字体并且不想每次都重新提交我的应用程序。

我按照其他一些指南尝试从 URL 下载字体文件,将其保存到应用程序的文档目录,然后使用 CGFontCreateWithDataProvider & CTFontManagerRegisterGraphicsFont 加载它。

但是,该应用程序始终无法执行“writeToFile”功能并输出以下行:

println("Failed to save font: \(fontFile)")

我已验证“fontUrl”有效(从调试器复制并粘贴到我的桌面浏览器中)。

此外,这是“fontPath”的值最终出现在调试器中的值,我认为它是正确的:

fontPath = "/Users/James/Library/Developer/CoreSimulator/Devices/E153E1F4-E3FD-4BCE-A40F-433EADF14591/data/Containers/Data/Application/969A248C-A7B3-4768-86E4-68A6C4E20ECF/Documents/Fonts/american_typewriter_medium_bt.ttf"

代码:

// Download and save font file
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let documentsDirectory = paths[0] as? String {
let fontPath = documentsDirectory.stringByAppendingPathComponent("Fonts/"+fontFile) as String
let fontUrl = NSURL(string: ApiUrlString + "fonts/" + fontFile)
if let fontData = NSData(contentsOfURL: fontUrl!)
{
if(!fontData.writeToFile(fontPath, atomically: true))
{
println("Failed to save font: \(fontFile)")
return
}
}
else
{
println("Failed to download font from URL: \(fontUrl)")
return
}

// Load Font
let data: NSData? = NSFileManager.defaultManager().contentsAtPath(fontPath)
if (data == nil)
{
println("Failed to load saved font: \(fontFile)")
return
}

var error: Unmanaged<CFError>?
let provider: CGDataProviderRef = CGDataProviderCreateWithCFData(data)
let font: CGFontRef = CGFontCreateWithDataProvider(provider)
if (!CTFontManagerRegisterGraphicsFont(font, &error))
{
println("Failed to register font, error: \(error)")
return
}

println("Successfully saved and registered font: \(fontFile)")
}
}
}
else
{
println("Failed to load documents directory: \(fontFile)")
}

最佳答案

我不确定这是否对您有帮助,但可能有帮助。本站iOS Font List提到使用可下载的字体。也许示例代码可以引导您朝着正确的方向前进。

- (void)asynchronouslySetFontName:(NSString *)fontName toTextView:(UITextView *)textView {
CGFloat size = 24.0f;
UIFont *font = [UIFont fontWithName:fontName size:size];

if (font && ([font.fontName compare:fontName] == NSOrderedSame || [font.familyName compare:fontName] == NSOrderedSame)) {
textView.font = font;
return;
}

NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObject:fontName forKey:kCTFontNameAttribute];
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);

NSMutableArray *descs = [NSMutableArray array];
[descs addObject:(__bridge id)desc];
CFRelease(desc);

__weak UITextView *weakTextView = textView;

CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) {
if (state == kCTFontDescriptorMatchingDidFinish) {
dispatch_async(dispatch_get_main_queue(), ^{
weakTextView.font = [UIFont fontWithName:fontName size:size];
});
}

return YES;
});
}

关于iOS - 下载动态字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com