0

This code work perfectly.
But I have a problem that I want to load HTML data and change it's font.
And I try to using NSAttributedString to set UIFont, it doesn't work to me.
What should I do to change HTML data font, and calculate it's height correct?
Thanks.

func loadHtmlContent() {

    let articleHtmlData = "<head><style>img{width:300px !important;height:225px !important}</style></head>"+articleContent

    do {
        let attrStr = try NSMutableAttributedString(
            data: articleHtmlData.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)

        attrStr.enumerateAttribute(
            NSFontAttributeName,
            in:NSMakeRange(0,attrStr.length),
            options:.longestEffectiveRangeNotRequired) { value, range, stop in
                let f1 = value as! UIFont
                let f2 = UIFont(name:"Helvetica", size:20)!
                if let f3 = applyTraitsFromFont(f1, to:f2) {
                    attrStr.addAttribute(
                        NSFontAttributeName, value:f3, range:range)
                }
        }

        self.articleHeight = self.heightForHtmlString(attrStr)
        self.articleContentTextView.attributedText = attrStr

    } catch let error {
        print(error)
    }

}

func heightForHtmlString(_ text: NSAttributedString) -> CGFloat {
    let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width-(16*4), height: CGFloat.greatestFiniteMagnitude))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = UIFont(name: "PingFangTC-Regular", size: 20.0)
    label.attributedText = text
    label.sizeToFit()
    return label.frame.height
}
5
  • 1
    Don't do label.font = UIFont(name: "PingFangTC-Regular", size: 20.0) when playing with label.attributedText. Use it only if you play with label.text. Commented Feb 23, 2018 at 9:11
  • Instead use the same logic as there: stackoverflow.com/a/41413014/1801544 Enumerate the FontAttributeName Commented Feb 23, 2018 at 9:13
  • @Larme (label.font) it just calculate height. Commented Feb 23, 2018 at 9:30
  • @Larme I update question, and I use enumerateAttribute but textView doesn't show any. Commented Feb 23, 2018 at 9:39
  • Just make attrStr a NSMutableAttributeString instead of a NSAttributedString (in the init). attrStr.addAttribute(NSFontAttributeName, value:UIFont(name: "PingFangTC-Regular", size: 20.0), range:NSMakeRange(0,attrStr.length)) Also, it's useless to set NSFontAttributeName : UIFont.systemFont(ofSize: 20.0) in the options: because it won't be taken account (see the doc of that method). Commented Feb 23, 2018 at 9:42

1 Answer 1

1
 ->   You can try this

let myDescriptionHTML = "<html><head> <style type=\"text/css\"> body{color:red;}<style>img{display:inline;height:auto;max-width:100%;}body {font-family:\"Eurostile LT\";font-size:20;} p {margin:0px!important; color:black !important;}</style></head><body>\(Your Word)</body></html>" as String

 print(myDescriptionHTML)
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, When I set "font-size:25" or up, my textView doesn't show any text. Why?
I checked this code in my project it is work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.