What is the best way to determine the supported styles of a font on iOS?
I'm unable to find an easy way to determine if a font supports the bold or italic font style on iOS. My current solution is based on trying to 开发者_运维技巧create a CTFont with the wished font style:
CTFontRef ctFont = CTFontCreateWithName(fontName, pointSize, NULL);
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(ctFont, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
if (boldFont) {
// supports bold font face
}
While this works fine, it somehow doesn't feel like the best way to do it. Is there a better way?
If you use UIFont I guess you could just iterate the fontNamesForFamilyName:
for (NSString *fontName : [UIFont fontNamesForFamilyName:fontName]) {
if ([fontName rangeOfString:@"Bold"].location != NSNotFound) {
//supports bold font face
}
}
Maybe not that much better... And a bold font isn't always defined with the text 'Bold', some use light and medium. http://www.iphonedevsdk.com/forum/iphone-sdk-development/6000-list-uifonts-available.html
Sometimes it's hard to even figure out the family name, especially for custom fonts. Usually just print out all the fonts to check for available fonts. You can figure out all the styles e.g. Bold, italic, condensed bold etc, from this
NSArray *familyNames = [UIFont familyNames];
for (NSString *familyName in familyNames)
{
NSSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
NSLog(@"%@", fontNames);
}
精彩评论