简体   繁体   中英

Change Attributed Text Font Size and Keep Formatting

I am creating an attributed string from text received from JSON. I'm then putting that text into a text view.

    let descriptionVC = BenefitDescriptionViewController()

    let attributedString = try! NSAttributedString(data: descriptionString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

    descriptionVC.labelString = attributedString

descriptionVC

override func viewDidLoad() {
        super.viewDidLoad()

        // print("Attributed String: \(labelString)")

        if let descriptionString = labelString {
            descriptionTextView.attributedText = descriptionString
            descriptionTextView.textColor = UIColor.white
            descriptionTextView.font = UIFont.systemFont(ofSize: 17.0)
        }
    }

I want to keep formatting, such as some of the text being bold, but make the font bigger. When I try to increase the font size, all of the other formatting goes away.

This is what I get when I print labelString.

在此处输入图片说明

You need to find the range and add attribute for that range. Suppose, "This is a test". In this text you want to display "This" as red and bold and "Test" with font size 40 and blue color, then find the range of "This" and add attribute(bold, red color) for that range. Again find the range of "Test" and add attribute (font size, blue color) for that range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM