Set bold to one UITextView iOS swift
Solution:
The below function is used for set the bold to the TextView.
The below function is used for set the bold to the TextView.
@IBAction func didTapOnBold(_ sender: Any) {
let textView = TextView
if textView != nil {
var fonts = textView.font
fonts = (fonts?.isBold)! ? fonts?.removeBold() : fonts?.setBold()
textView.font = fonts!.fontName
}
}
extension UIFont
{
var isBold: Bool
{
return fontDescriptor.symbolicTraits.contains(.traitBold)
}
//set the bold
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)
}
}
//remove the bold
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)
}
}
}
Comments
Post a Comment