我试图使用属性字符串在UILabel上加载一个简单的html字符串(有一个表),如果语言是英语,则它对于rtl布局效果很好,但格式不起作用,但是在WKWebView上效果很好,这是样品
let testStr = "
\"514.0\" >
عمود 1عمود 2عمود 3عمود4
عمود 5صف 1 .صف 2."
testLbl.attributedText =
testStr.toHtmlAttributedText()
extension String{
func toHtmlAttributedText() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf8,
allowLossyConversion: true) else { return nil }
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
NSAttributedString.DocumentReadingOptionKey.characterEncoding : String.Encoding.utf8.rawValue,
NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,
]
let htmlString = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil)
//this to have borders in html table
htmlString?.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.clear, range: NSMakeRange(0, 1))
return htmlString
}
}
这是它在模拟器和html编辑器上的外观