Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

ios8 - Font Descriptor returns nil in iOS 8

The following code works fine in iOS 7, but doesn't return bold or italic font in iOS 8. It is ok for Helvetica Neue, but doesn't work for Arial font.

UIFontDescriptor *descriptor1 = [UIFontDescriptor fontDescriptorWithFontAttributes:@{UIFontDescriptorFamilyAttribute: @"Arial"}];
UIFontDescriptor* boldFontDescriptor1 = [descriptor1 fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFont* font1 = [UIFont fontWithDescriptor:boldFontDescriptor1 size:16.0];
[self.lblArialB setFont:font1];

Tested on device and simulator and still same error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...