我做了很多搜索,无法找出在UITextView中添加html内容的正确方法。以下是我的代码。没有设置正确的字体类型。
let htmlProfileString = "<html><head><style>body {font-family:\"SFProText-Regular\";font-size: 16px;color: #FFFFFF;text-decoration:none;}</style></head><body>" +
"This is content"
+ "</body></head></html>"
let htmlData = NSString(string: htmlProfileString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil)
self.textView.attributedText = attributedString
答案 0 :(得分:0)
如果您不希望textView
可编辑,则将其替换为webView
。
如果这样做,则可以使用以下方法将html加载到webView中:
func loadHTMLString(_ string: String,
baseURL: URL?)
进一步的参考:https://developer.apple.com/documentation/uikit/uiwebview/1617979-loadhtmlstring
答案 1 :(得分:0)
要使用“系统”旧金山字体,请使用-apple-system
:
let htmlProfileString = "<html><head><style>body {font-family:\"-apple-system\";font-size: 16px;color: #FFFFFF;text-decoration:none;}</style></head><body>" +
"This is content"
+ "</body></head></html>"
您还可以将其他CSS字体样式用于:
font: -apple-system-body
font: -apple-system-headline
font: -apple-system-subheadline
font: -apple-system-caption1
font: -apple-system-caption2
font: -apple-system-footnote
font: -apple-system-short-body
font: -apple-system-short-headline
font: -apple-system-short-subheadline
font: -apple-system-short-caption1
font: -apple-system-short-footnote
font: -apple-system-tall-body