sách gpt4 ăn đã đi

iphone - 如何将 CGFontRef 转换为 UIFont?

In lại 作者:技术小花猫 更新时间:2023-10-29 10:06:25 31 4
mua khóa gpt4 giày nike

我想为 UILabel 使用自定义字体。自定义字体由文件加载:

NSString *fontPath = ... ; // a TTF file in iPhone Documents folder
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);

làm cách nào để CGFontRef Chuyển đổi thành UIFont 以便在 [UILabel setFont:] 中使用?

câu trả lời hay nhất

Bạn không thể CGFontRef 直接转换为 UIFont 但您可以使用 CTFontManagerRegisterGraphicsFont 注册 CGFontRef 然后创建对应的UIFont.

NSString* fpath = [documentsDirectory stringByAppendingPathComponent:@"custom_font_file_name.ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fpath UTF8String]);
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(customFont);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(customFont, &error);
CGFontRelease(customFont);
UIFont* uifont = [UIFont fontWithName:fontName size:12];

关于iphone - 如何将 CGFontRef 转换为 UIFont?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9205709/

31 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress