How to render HTML string in UITextview with different font styles but same font family

為{幸葍}努か 提交于 2019-12-08 05:16:08

问题


This is my HTML string:

<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>

I want all the styling to be same (Italic, bold etc), but I want to implement the font family as Open Sans. So, wherever its italics I want the font to be Open-Sans Italics. How do I implement that? Please help.


回答1:


One way to achieve the desired behavior is this (copy-and-paste to a playground).

import PlaygroundSupport
import UIKit

func attributedString(fromHTML html: String, fontFamily: String) -> NSAttributedString? {
    let prefix = "<style>* { font-family: \(fontFamily) }</style>"
    return (prefix + html).data(using: .utf8).map {
        try! NSMutableAttributedString(
            data: $0,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil
        )
    }
}

let attributedText = attributedString(fromHTML: "<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>", fontFamily: "Courier")

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
label.backgroundColor = .white
label.attributedText = attributedText
PlaygroundPage.current.liveView = label




回答2:


try using NSAttributedString with NSHTMLTextDocumentType option and .utf8 string encoding

Swift 3:

extension String {

    var htmlAttributedString: NSAttributedString? {

        do {
            return try NSAttributedString(data: data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch let error {
            print(error.localizedDescription)
        }
        return nil
    }
}

Use

let htmlStringCode = "<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>"

if let htmlText = htmlStringCode.htmlAttributedString {
       let attributes = [NSFontAttributeName: UIFont(name: "Open Sans", size: 18.0)!]
        let attributedText = NSMutableAttributedString(string: htmlText.string, attributes: attributes)

        let htmlString = htmlText.string as NSString
        let range  = htmlString.range(of: "Italics")
        attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "OpenSans-Italic", size: 18.0)!, range: range)

        textView.attributedText = attributedText
}



回答3:


I had to add my own CSS. This solved my problem:

UIFont *postFont = [UIFont fontWithName:@"OpenSans" size:13.0];

NSString *postWithFont = [self.msgHtml stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}i{font-family: '%@'; font-size:%fpx;}</style>", postFont.fontName, postFont.pointSize, [postFont.fontName stringByAppendingString:@"-Italic"], postFont.pointSize]];

_mainText = [[NSMutableAttributedString alloc]
                                       initWithData:[postWithFont dataUsingEncoding:NSUTF8StringEncoding]
                                       options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                 NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                       documentAttributes:nil
                                       error:nil];


来源:https://stackoverflow.com/questions/41691743/how-to-render-html-string-in-uitextview-with-different-font-styles-but-same-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!