Font functions as extension in iOS swift || Bold add,Bold remove,Italic add,Italic remove functions
Solution:
The below function used for add and remove the bold and italic to a particular text
extension UIFont
{
var isBold: Bool
{
return fontDescriptor.symbolicTraits.contains(.traitBold)
}
var isItalic: Bool
{
return fontDescriptor.symbolicTraits.contains(.traitItalic)
}
func setBold() -> UIFont
{
if isBold {
return self
} else {
var symTraits = fontDescriptor.symbolicTraits
symTraits.insert([.traitBold])
let fontDescriptorVar = fontDescriptor.withSymbolicTraits(symTraits)
return UIFont(descriptor: fontDescriptorVar!, size: 0)
}
}
func setItalic()-> UIFont
{
if isItalic {
return self
} else {
var symTraits = fontDescriptor.symbolicTraits
symTraits.insert([.traitItalic])
let fontDescriptorVar = fontDescriptor.withSymbolicTraits(symTraits)
return UIFont(descriptor: fontDescriptorVar!, size: 0)
} }
func removeBold()-> UIFont
{
if !isBold {
return self
} else {
var symTraits = fontDescriptor.symbolicTraits
symTraits.remove([.traitBold])
let fontDescriptorVar = fontDescriptor.withSymbolicTraits(symTraits)
return UIFont(descriptor: fontDescriptorVar!, size: 0)
}
}
func removeitalic()-> UIFont
{
if !isItalic {
return self
} else {
var symTraits = fontDescriptor.symbolicTraits
symTraits.remove([.traitItalic])
let fontDescriptorVar = fontDescriptor.withSymbolicTraits(symTraits)
return UIFont(descriptor: fontDescriptorVar!, size: 0)
}
}
}
Comments
Post a Comment