在iOS上确定支持的字体样式的最佳方法是什么?

时间:2011-04-27 15:03:36

标签: cocoa-touch ios uifont

我无法找到一种简单的方法来确定字体是否支持iOS上的粗体或斜体字体样式。我目前的解决方案是基于尝试使用希望的字体样式创建CTFont:

CTFontRef ctFont = CTFontCreateWithName(fontName, pointSize, NULL);
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(ctFont, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
if (boldFont) {
// supports bold font face
}

虽然这种方法很好,但它在某种程度上并不是最好的方法。有没有更好的办法?

2 个答案:

答案 0 :(得分:2)

如果你使用UIFont,我想你可以只迭代fontNamesForFamilyName:

for (NSString *fontName : [UIFont fontNamesForFamilyName:fontName]) {
    if ([fontName rangeOfString:@"Bold"].location != NSNotFound) {
       //supports bold font face
    }
}

也许没有那么好......而且粗体字并不总是用文字'Bold'来定义,有些使用光和中等。 http://www.iphonedevsdk.com/forum/iphone-sdk-development/6000-list-uifonts-available.html

答案 1 :(得分:1)

有时甚至很难找出姓氏,尤其是自定义字体。通常只打印出​​所有字体以检查可用的字体。你可以弄清楚所有的风格,例如大胆,斜体,浓缩粗体等,来自此

NSArray *familyNames = [UIFont familyNames];
for (NSString *familyName in familyNames)
{
    NSSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
    NSLog(@"%@", fontNames);
}