【发布时间】:2020-04-15 07:44:53
【问题描述】:
我正在尝试从给定的 HTML 字符串创建 NSAttributedString,带有段落和超链接。一切都很好,除了样式超链接。 我的样式代码如下所示:
func htmlAttributed() -> NSAttributedString? {
do {
let htmlCSSString = "<style>" +
"* {" +
"font-size: 12px !important;" +
"color: #9D9B98 !important;" +
"}" +
"p" +
"{" +
"font-size: 12px !important;" +
"color: #9D9B98 !important;" +
"font-family: Helvetica !important;" +
"}" +
"a" +
"{" +
"font-size: 12px !important;" +
"color: hotpink !important;" +
"font-family: Helvetica !important;" +
"}" +
"a:link" +
"{" +
"text-decoration: none !important;" +
"color: hotpink !important;" +
"}" +
"a:visited" +
"{" +
"text-decoration: none !important;" +
"}" +
"</style> \(self)"
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
当我在网站上打开它时效果很好。但是转换为 NSAttributedString 仅适用于 p 标记,不适用于超链接。
为了测试,我使用这部分代码
<p>This is not a link <b><a class='test' href='test' target='_blank'>This is a link</a></b></p>
在 iPhone 上我有:
【问题讨论】:
标签: ios swift iphone nsattributedstring