Font Descriptor returns nil in iOS 8
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];
swift alternative for fontDescriptorWithSymbolicTraits ios 8 bug
You're right about this. The bug was fixed in iOS 8.3, fortunately. The only reliable workaround is to drop down to the level of Core Text and perform the trait application there. It's a pain, but it solves the problem.
Thus, for instance, before iOS 8.3, this doesn't work:
if let body = UIFont(name: "GillSans", size: 15),
emphasis = body.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic) {
fbody = body
femphasis = UIFont(descriptor: emphasis, size: 0)
}
So you have to import CoreText
and drop down to that level:
if let body = UIFont(name: "GillSans", size: 15),
result = CTFontCreateCopyWithSymbolicTraits(body as CTFont, 0, nil, .ItalicTrait, .ItalicTrait) {
fbody = body
femphasis = result as UIFont
}
Fortunately, Swift 1.2 and later is aware that UIFont and CTFont are now toll-free bridged. Before Swift 1.2, this was even more complicated! And of course in earlier systems, they were not toll-free bridged and this was still more difficult.
ios11- UIFont return nil
Try to check the available fonts
for familyName:String in UIFont.familyNames {
print("Family Name: \(familyName)")
for fontName:String in UIFont.fontNames(forFamilyName: familyName) {
print("--Font Name: \(fontName)")
}
}
May be the font spelling is different.
Custom font iOS in Today Widget always return nil
Maybe it's because you forgot to add key in your .plist
file.
Add the key Fonts provided by application
to a new row. Add items for each font you have added.
Add custom weight to iOS Font descriptor in Swift
UIFont(descriptor: imgFontDescriptor!, size: 24.0)
is returning a Font what match with the descriptor. If it can't find a Font match with your description, it returns a default font. Therefore, you can't control your weight manually. It depends on the Font you use. If the Font is support that weight, it will return that.
One more thing, you should use [UIFontDescriptorFamilyAttribute: "Helvetica"]
. So it will determine FontName base on FamilyName & your FontWeight.
The correct way is use the constant from Apple lib:
public let UIFontWeightUltraLight: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightThin: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightLight: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightRegular: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightMedium: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightSemibold: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightBold: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightHeavy: CGFloat
@available(iOS 8.2, *)
public let UIFontWeightBlack: CGFloat*/
You should use http://iosfonts.com/ to determine which font weight that family name is supporting.
In case of Helvetica:
let traits = [UIFontWeightTrait: UIFontWeightLight] // UIFontWeightBold / UIFontWeightRegular
let imgFontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorFamilyAttribute: "Helvetica"])
imgFontDescriptor = imgFontDescriptor.fontDescriptorByAddingAttributes([UIFontDescriptorTraitsAttribute: traits])
custom font becomes a nil value in swift 3
Try this
let attribute1:[String:Any] = [NSFontAttributeName: UIFont(name: "Bariol-Bold", size: 18)! ,
NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0)]
let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)
Why do some iOS font families not support UIFontDescriptorTraitBold?
This is a bug. fontDescriptorWithSymbolicTraits:
is guaranteed to return a font descriptor; returning nil
is therefore unexpected behavior.
In fact, if you rewrite the same thing with Swift, it will cause a crash because desc
isn't optional:
var desc = UIFontDescriptor()
desc = desc.fontDescriptorWithFamily("Times New Roman")
desc = desc.fontDescriptorWithSymbolicTraits(.TraitBold)
println(desc); //crash
Whether or not the UIFontDescriptor
would return a UIFont
is a separate question. You should file a radar.
Related Topics
Delete and Update Data in Core Data in iOS
Realm Accessed from Incorrect Thread
Google Maps Url Scheme Not Working on iOS 9
Firebase Pod Install - Pod 'Firebase/Database' - Required a Higher Minimum Deployment Target
Change Button Title Color in Uialertview
Open iOS 11 Files App via Url Scheme or Some Other Way
<Googlemaps/Googlemaps.H> File Not Found Google Maps Sdk for iOS
Uipickerview: Nsattributedstring Not Available in iOS 7
Xcode 7 Crash: [Nslocalizablestring Length] 30000
Play a Video from Youtube in a Avplayerviewcontroller in Swift
Non-Nominal Type X Does Not Support Explicit Initialization
How to Mute/Unmute Audio When Playing Video Using Mpmovieplayercontroller