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.