轉自http://bj007.blog.51cto.com/1701577/548932
iPhone上的系統字體在很多場合都可以滿足要求了。你可以通過系統提供的字體類方法輕鬆地在普通、加粗和斜體之間選擇。示例代碼如下:
UIFont *mainTitleFont = [UIFont boldSystemFontOfSize:14.0];
UIFont *subTitleFont = [UIFont SystemFontOfSize:14.0];
UIFont *textFont = [UIFont italicSystemFontOfSize:12.0];
但如果要一個加粗斜體或者其他字體怎麼辦呢?在這種情況下,你可以按如下使用fontWithName方法:
UIFont *altFont = [UIFont fontWithName:@"Courier-Bold" size:14.0];
這很好,不過你如何知道可用的字體名字呢?如果你猜錯了字體名字,你的代碼會拋出異常——這裏可沒有映射到最接近字體的優雅處理!
在iPhone文檔上我無法找到可用的字體名字,在網絡上搜索的時候也無果(至少在搜索結果的前幾頁)。所以我寫了如下一段代碼:
// 列出iPhone上所有的字體 // List all fonts on iPhone
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(@" Font name: %@", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];
如果使用iPhone SDK v2.1輸出結果如下:
Family name: Hiragino Kaku Gothic ProN W3
Font name: HiraKakuProN-W3
Family name: Courier
Font name: Courier
Font name: Courier-BoldOblique
Font name: Courier-Oblique
Font name: Courier-Bold
Family name: Arial
Font name: ArialMT
Font name: Arial-BoldMT
Font name: Arial-BoldItalicMT
Font name: Arial-ItalicMT
Family name: STHeiti TC
Font name: STHeitiTC-Light
Font name: STHeitiTC-Medium
Family name: AppleGothic
Font name: AppleGothic
Family name: Courier New
Font name: CourierNewPS-BoldMT
Font name: CourierNewPS-ItalicMT
Font name: CourierNewPS-BoldItalicMT
Font name: CourierNewPSMT
Family name: Zapfino
Font name: Zapfino
Family name: Hiragino Kaku Gothic ProN W6
Font name: HiraKakuProN-W6
Family name: Arial Unicode MS
Font name: ArialUnicodeMS
Family name: STHeiti SC
Font name: STHeitiSC-Medium
Font name: STHeitiSC-Light
Family name: American Typewriter
Font name: AmericanTypewriter
Font name: AmericanTypewriter-Bold
Family name: Helvetica
Font name: Helvetica-Oblique
Font name: Helvetica-BoldOblique
Font name: Helvetica
Font name: Helvetica-Bold
Family name: Marker Felt
Font name: MarkerFelt-Thin
Family name: Helvetica Neue
Font name: HelveticaNeue
Font name: HelveticaNeue-Bold
Family name: DB LCD Temp
Font name: DBLCDTempBlack
Family name: Verdana
Font name: Verdana-Bold
Font name: Verdana-BoldItalic
Font name: Verdana
Font name: Verdana-Italic
Family name: Times New Roman
Font name: TimesNewRomanPSMT
Font name: TimesNewRomanPS-BoldMT
Font name: TimesNewRomanPS-BoldItalicMT
Font name: TimesNewRomanPS-ItalicMT
Family name: Georgia
Font name: Georgia-Bold
Font name: Georgia
Font name: Georgia-BoldItalic
Font name: Georgia-Italic
Family name: STHeiti J
Font name: STHeitiJ-Medium
Font name: STHeitiJ-Light
Family name: Arial Rounded MT Bold
Font name: ArialRoundedMTBold
Family name: Trebuchet MS
Font name: TrebuchetMS-Italic
Font name: TrebuchetMS
Font name: Trebuchet-BoldItalic
Font name: TrebuchetMS-Bold
Family name: STHeiti K
Font name: STHeitiK-Medium
Font name: STHeitiK-Light
在選用非系統字體的時候,你需要確保選擇的字體在列表中。在將來的iPhone OS中字體列表是否會改變還有待觀察。建議重新運行下上述代碼段進行確認。