本文介绍了如何将CGFontRef转换成UIFont?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为 UILabel
使用自定义字体。自定义字体由文件加载:
I want to use a custom font for a UILabel
. The custom font is loaded by from a file:
NSString *fontPath = ... ; // a TTF file in iPhone Documents folder
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
如何将 CGFontRef
转换为a UIFont
用于 [UILabel setFont:]
?
How can I convert the CGFontRef
to a UIFont
to be used in [UILabel setFont:]
?
推荐答案
康拉德的答案很接近但不太有效。您需要为UIFont提供PostScript名称,而不是全名。
Conrad's answer is close but doesn't quite work. You need to provide UIFont with the PostScript name, rather than the full name.
NSString *fontName = (NSString *)CGFontCopyPostScriptName(fontRef);
UIFont *font = [UIFont fontWithName:fontName size:someSize]
这篇关于如何将CGFontRef转换成UIFont?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!