我尝试使用给定字体系列名称来获得粗体 iOS UIFont 的方法似乎仅适用于某些字体。例如:
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)
这是一个错误,还是它不适合每个字体系列(实际上有一个粗体变体)?我真的不想被迫解析字体名称以寻找“粗体”或类似的东西。