Font Descriptor returns nil in iOS 8

拟墨画扇 提交于 2019-11-28 12:21:42

FWIW, this is the workaround I came up with, using UIFontDescriptor's attributes dictionary initializer instead of the seemingly buggy fontDescriptorWithSymbolicTraits:

NSString *fontFamily = @"Arial";
BOOL isBold = YES;
BOOL isItalic = YES;
CGFloat fontSize = 20.0;
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:
                  @{
                    @"NSFontFamilyAttribute" : fontFamily,
                    @"NSFontFaceAttribute" : (isBold && isItalic ? @"Bold Italic" : (isBold ? @"Bold" : (isItalic ? @"Italic" : @"Regular")))
                    }];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];

For me, the fontDescriptorWithFontAttributes is working as before but the fontDescriptorWithSymbolicTraits fails miserably. I solved it by going back to using [UIFont fontWithName: size:]

[self.titleLabel setFont:[UIFont fontWithName:@"PTSans-Bold" size:47.67]];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!