iosswiftuifontbolditalic

Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes


I am surprised, that simply setting bold and italic for existing font is so complicated in Swift.

I just want to simplify things by having following methods on font class.

I want the below methods to be added to existing font which has font-family and font-size set. I need to preserve these and change only the following.

setBold : Shud preserve italic

setItalic : Shud preserve bold

setBoldItalic

setNormal : remove both bold and italic

removeBold : Shud preserve italic

removeitalic : Shud preserve bold

I tried the below, and it's feeling like a nightmare for me using fontDescriptorWithSymbolicTraits.

Is there a simpler way of doing these in a few lines of code?

extension UIFont
{
    var isBold: Bool
    {
        return fontDescriptor().symbolicTraits.contains(.TraitBold)
    }

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

    func setBold() -> UIFont
    {
        var fontDescriptorVar: UIFontDescriptor
        if(isBold){
            return self
        }
        else
        {
            fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
        }
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    func setItalic()-> UIFont
    {
        var fontDescriptorVar: UIFontDescriptor
        if(isItalic) {
            return self
        }
        else
        {
            fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic)
        }
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    func setBoldItalic()-> UIFont
    {
        let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(arrayLiteral: .TraitBold, .TraitItalic))
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    // Things I need are

    // To set back to normal

    func setNormal()-> UIFont
    {

    }

    // Remove only bold if it's both bold and Italic

    func removeBold()-> UIFont
    {

    }

    // Remove only italic if it's both bold and Italic

    func removeitalic()-> UIFont
    {

    }
}

I don't want to use this which asks me the size and font as input:

UIFont(name "namFontFamily", size: 16)

UIFont.systemFontOfSize(16, weight: UIFontWeightLight)

I searched everywhere and found no simple solution matching my needs.


Solution

  • You say you want to preserve other traits, so you may need to modify some methods in your code:

    func setBold() -> UIFont
    {
        if isBold {
            return self
        } else {
            var symTraits = fontDescriptor().symbolicTraits
            symTraits.insert([.TraitBold])
            let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(symTraits)
            return UIFont(descriptor: fontDescriptorVar, size: 0)
        }
    }
    

    setItalic() and setBoldItalic() as well.

    So, removeBold() should be something like this:

    func removeBold()-> UIFont
    {
        if !isBold {
            return self
        } else {
            var symTraits = fontDescriptor().symbolicTraits
            symTraits.remove([.TraitBold])
            let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(symTraits)
            return UIFont(descriptor: fontDescriptorVar, size: 0)
        }
    }
    

    removeItalic() would be similar.

    But I'm not sure about setNormal(). You want remove all traits, or want to just remove italic and bold? Maybe you can do it yourself as you like.