I was trying to load a simple html string (which has a table) on the UILabel using the attributed string, if the language is english it works well for the rtl layouts the formatting does not work, but it works well on WKWebView, here is the sample
let testStr = "<html dir = \"RTL\"> <table border=\"1\" width =
\"514.0\" ><tr><td>عمود 1</td><td>عمود 2</td><td>عمود 3</td><td>عمود
4</td><td>عمود 5</td></tr><tr><td>صف 1</td><td><br></td><td><br></td> .
<td><br></td><td><br></td></tr><tr><td>صف 2</td><td><br></td><td><br> .
</td><td><br></td><td><br></td></tr></table></html>"
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
}
}
Here is how it looks on simulator and on html editor