我試圖用一個字體系列名稱來獲得大膽面對的iOS UIFont的方法似乎只適用於某些字體。例如:爲什麼一些iOS字體系列不支持UIFontDescriptorTraitBold?
UIFont* font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
NSLog(@"A: Font name: %@", [font fontName]);
// desired method works for Helvetica Neue
UIFontDescriptor *desc = [[UIFontDescriptor alloc] init];
desc = [desc fontDescriptorWithFamily:@"Helvetica Neue"];
desc = [desc fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
font = [UIFont fontWithDescriptor:desc size:12];
NSLog(@"B: Font name: %@", [font fontName]);
// desired method fails for Times New Roman
desc = [[UIFontDescriptor alloc] init];
desc = [desc fontDescriptorWithFamily:@"Times New Roman"];
NSLog(@"desc: %@", desc);
desc = [desc fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
NSLog(@"desc bold: %@", desc);
打印(在iOS 8.1模擬器):
A: Font name: TimesNewRomanPS-BoldMT
B: Font name: HelveticaNeue-Bold
desc: UICTFontDescriptor <0x7f9be0d05e80> = { NSFontFamilyAttribute = "Times New Roman"; }
desc bold: (null)
這是一個錯誤,或者是它的設計不適用於所有的字體家族工作(實際上有一個大膽的變體)?我真的不希望被迫解析字體名稱尋找「大膽」或這樣的事情。
我這個fontDescriptorWithSymbolicTraits的另一個受害者:錯誤。這是一個恥辱,因爲它可以很好地處理iOS7上的任何字體。在此期間你找到了一個聰明的解決方法嗎?我不想爲「-Black | -Bold | -Italic | -Oblique」解析字體名稱。 – Bluezen 2015-01-02 19:07:31