0

I wrote some code to make a string bold, italic, underlined and strikethrough.

But while performing these functions, I want to keep a specific font name and size intact and not default it to system values for both font name and font size. How do I achieve that?

Below is the code added for bold, italic, underline and strikethrough:

if textBold == true {
    let string = text
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.boldSystemFont(ofSize: 25)
    ]
    let attributedString = NSAttributedString(string: string, attributes: attributes)
    modifiedString = attributedString
    text_View.attributedText = modifiedString
    text_View.sizeToFit()
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textItalic == true {
    text_View.sizeToFit()
    let string = text
    
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
    ]
    let attributedString = NSAttributedString(string: string, attributes: attributes)
    self.modifiedString = attributedString
    text_View.attributedText = modifiedString
    
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textUnderline == true {
    text_View.sizeToFit()
    let string = text
    let attributedString = NSMutableAttributedString.init(string: string)
    attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
                                    NSRange.init(location: 0, length: attributedString.length))
    self.modifiedString = attributedString
    text_View.attributedText = modifiedString
    
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textStrikethrough == true {
    text_View.sizeToFit()
    let string = text
    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: string)
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
    self.modifiedString = attributeString
    text_View.attributedText = modifiedString
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}
TylerP
  • 9,600
  • 4
  • 39
  • 43
iosDevSan
  • 65
  • 1
  • 13
  • Your question is illogical. How do you know that the font family you choose contains bold and italic typefaces? Besides, UIKit doesn't have a font manager like the one that is available in Cocoa. – El Tomato Dec 14 '21 at 07:42
  • Just check this https://stackoverflow.com/questions/18862868/setting-bold-font-on-ios-uilabel – David Prusa Dec 14 '21 at 11:34

1 Answers1

0
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "name of your font", size: 12.0)]

or create extension of UIFont

extension UIFont {
    class func customFont(size: CGFloat) -> UIFont {
        guard let font = UIFont(name: "font name", size: 12.0) else {
            return UIFont.systemFont(ofSize: 12.0)
        }
        return font
    }
}

use it like :

let attributedString = NSAttributedString(string: string, attributes: [NSAttributedString.Key.font: UIFont.customFont(size: 12.0)])
Shabnam Siddiqui
  • 579
  • 4
  • 13