4

I have been struggling for a while now about this fontDescriptor.withSymbolicTraits it returns nil whenever I try to use Bold or Italic.

I'm using this code below:

func withTraits(traits:UIFontDescriptor.SymbolicTraits) -> UIFont {
    let descriptor = fontDescriptor.withSymbolicTraits(traits)
    return UIFont(descriptor: descriptor!, size: 0) //size 0 means keep the size as it is }

func bold() -> UIFont {
    return withTraits(traits: .traitBold) }

func italic() -> UIFont {
    return withTraits(traits: .traitItalic) }

But the app stops and shows this error (this points to fontDescriptor.withSymbolicTraits):

Fatal error: Unexpectedly found nil while unwrapping an Optional value Fatal error: Unexpectedly found nil while unwrapping an Optional value

I also tried this code (which works on other devs as what I read online):

    var isBold: Bool {
        return fontDescriptor.symbolicTraits.contains(.traitBold)
    }

    var isItalic: Bool {
        return fontDescriptor.symbolicTraits.contains(.traitItalic)
    }

    func setBoldFnc() -> UIFont {
        if(isBold) {
            return self
        } else {
            var fontAtrAry = fontDescriptor.symbolicTraits
            fontAtrAry.insert([.traitBold])
            guard let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry) else { return self }
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

    func setItalicFnc()-> UIFont {
        if(isItalic) {
            return self
        } else {
            var fontAtrAry = fontDescriptor.symbolicTraits
            fontAtrAry.insert([.traitItalic])
            guard let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry) else { return self }
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

Anyone out there experiencing this in iOS 12 onwards?

I also read that there was a bug in iOS 8 regarding this, but says that this was fixed in the later version already:

https://code-examples.net/en/q/18c6177

Font Descriptor returns nil in iOS 8

*NOTE: I used custom font with only "-Regular" file. 'Cause some font doesn't have "-Italic" or "-Bold" font style.

Example: Archivo Black font and Oswald font

Pepeng Hapon
  • 357
  • 1
  • 17
  • unfortunately there is no 12.3.1 Simulator, is this also happening in 12.4? – Chris Oct 30 '19 at 09:52
  • i just tested this for 12.4 and it works...but I cannot test in on 12.3.1 and for ios 13 works too – Chris Oct 30 '19 at 10:24
  • Hi @Chris thanks for the comment. Have you tried using custom font without "-Italic" file? I forgot to add this in my question. 'Cause it seems it will not work for custom fonts with only "-Regular" style. I edited my question. – Pepeng Hapon Oct 30 '19 at 21:20
  • but according to this link https://fonts.google.com/specimen/Archivo+Black archivo black has italic style....and I do not think that Apple can magically give you a italic style from a regular - only font. – Chris Oct 31 '19 at 08:23

0 Answers0