首先:是否可以下载自定义字体并将其显示在UILabel中?
我正在使用此代码从Internet下载自定义字体并将其显示在UILabel中,但是它不起作用。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize txt_UserName;
- (void)viewDidLoad {
[super viewDidLoad];
// 1
NSString *dataUrl = @"https://dl.dafont.com/dl/?f=stars_fighters";
NSURL *url = [NSURL URLWithString:dataUrl];
// 2
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// 4: Handle response here
if (error == nil) {
NSLog(@"no error!");
if (data != nil) {
NSLog(@"There is data!");
[self loadFont:data];
}
} else {
NSLog(@"%@", error.localizedDescription);
}
}];
// 3
[downloadTask resume];
}
- (void)loadFont:(NSData *)data
{
NSData *inData = data;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
// CFRelease(errorDescription);
}
// CFRelease(font);
// CFRelease(provider);
CTFontRef ctFont = CTFontCreateWithGraphicsFont(font, 30, NULL, NULL);
UIFont *uiFont = CFBridgingRelease(ctFont);
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt_UserName setFont:uiFont];
});
[self fontTest];
}
- (void)fontTest
{
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++) {
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
}
@end
无论如何,我遇到此错误: 无法加载字体:操作无法完成。无效的论点
在自定义字体的位置,我获得了默认的iOS字体。
当然,可以下载自定义字体并将其显示在UILabel中,这是我的代码,效果很好:
步骤1:假设字体文件已保存在沙箱路径中Documents / StarsFighters.ttf
,请加载字体并获取字体名称(在YOUR_CLASS .m文件中):
+ (NSString *)loadFontAndReturnFontName {
NSString *imgFilePath = [NSString stringWithFormat:@"%@/Documents/StarsFighters.ttf",NSHomeDirectory()];
NSURL *fontUrl = [NSURL fileURLWithPath:imgFilePath];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CTFontManagerRegisterGraphicsFont(fontRef, NULL);
NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
return fontName;
}
第二步:使用自定义字体
NSString *fontname = [YOUR_CLASS loadFontAndReturnFontName];
//fontname is @"StarsFighters",best set it to a global variable or save it
YOUR_LABEL.font = [UIFont fontWithName:fontname size:18.0];
谢谢!而要下载字体,您使用与我相同的代码吗?
另外,我在控制台上得到了此信息:CoreText注意:客户端请求的名称为“ .Helvetica NeueUI”,它将获得TimesNewRomanPSMT而不是预期的字体。所有系统UI字体访问均应通过适当的API进行,例如CTFontCreateUIFontForLanguage()或+ [UIFont systemFontOfSize:]。2020-02-03 09:23:09.522915 + 0100测试字体[285:6177] CoreText注意:在CTFontLogSystemFontNameRequest上设置一个断点以进行调试。
您的下载链接是一个zip文件,您需要在其中解压缩ttf文件。为了方便起见,我直接通过此链接进行了测试:
https://raw.githubusercontent.com/wooodypan/AlgorithmWithMac/master/AlgorithmWithMac/Stars%20Fighters.ttf
您可以
ZipArchive
用来解压缩zip文件:github.com/ZipArchive/ZipArchive