Sujay U N Sujay U N - 4 months ago 160
Swift Question

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 below methods to be added to existing font which has font-family and font-size set. I need to preserve these and change only 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 below and its feeling a nightmare for me using fontDescriptorWithSymbolicTraits

Is there any other simple way of doing these in 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 its both bold and Italic

func removeBold()-> UIFont
{

}

// Remove only italic if its both bold and Italic

func removeitalic()-> UIFont
{

}

}


I don't want to use this which asks me 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
Kindly help me. Hope this will help all

Answer

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.

Comments