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
}
tolerate_Me_Thx
  • 387
  • 2
  • 7
  • 21
  • 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`. – Larme Feb 23 '18 at 09:11
  • Instead use the same logic as there: https://stackoverflow.com/a/41413014/1801544 Enumerate the FontAttributeName – Larme Feb 23 '18 at 09:13
  • @Larme (label.font) it just calculate height. – tolerate_Me_Thx Feb 23 '18 at 09:30
  • @Larme I update question, and I use enumerateAttribute but textView doesn't show any. – tolerate_Me_Thx Feb 23 '18 at 09: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). – Larme Feb 23 '18 at 09:42

1 Answers1

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)
Dhaval Solanki
  • 71
  • 1
  • 2
  • 9