我试图用一个字体系列名称来获得大胆面对的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