gpt4 book ai didi

iphone - 如何将 CGFontRef 转换为 UIFont?

转载 作者:技术小花猫 更新时间:2023-10-29 10:06:25 28 4
gpt4 key购买 nike

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

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

如何将 CGFontRef 转换为 UIFont 以便在 [UILabel setFont:] 中使用?

最佳答案

您不能将 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/

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