问题
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