swift - UILabel 中使用 NSAttributedString 的 HTML 文本(带表格)

标签 swift uilabel nsattributedstring richtext nsattributedstringkey

我试图使用属性字符串在 UILabel 上加载一个简单的 html 字符串(其中有一个表格),如果语言是英语,它对于 rtl 布局效果很好,格式不起作用,但它在 WKWebView 上效果很好,这是示例

    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
    }
}

这是它在模拟器和 html 编辑器上的外观

][1]

][1]

最佳答案

尝试使用NSTextAligment

NSTextAligment 提供了多种文本视觉对齐情况。

在 UILabel 中,您可以将它们应用为:

label.textAlignment = .natural

.natural -> Indicates the default alignment for script.

提示:在文档中探索 NSTextAligment。 NSWritingDirection 是另一个可能对您有更多帮助的枚举。

其次,您还可以处理NSLinguisticTagScheme 确定输入语言并相应地支持适当的文本对齐。

示例代码如下:

func helpMeWithDirection(){
 let linguisticTagScheme = [NSLinguisticTagScheme.language]
    let linguicticTagger    = NSLinguisticTagger(tagSchemes: linguisticTagScheme, options: 0)
    linguicticTagger.string = self.text
    let currentLanguage = linguicticTagger.tag(at: 0, scheme: NSLinguisticTagScheme.language,
                                                    tokenRange: nil, sentenceRange: nil)

    if currentLanguage?.rawValue.range(of: "he") != nil ||  currentLanguage?.rawValue.range(of: "ar") != nil {
        self.textAlignment = NSTextAlignment.right
    } else {
        self.textAlignment = NSTextAlignment.left
    }
} 

上面的函数将是 UILabel 的扩展,因为您正在处理一个。 希望对你有帮助。谢谢!

PS:我假设您能够解析属性字符串,并且如图所示,唯一的问题是 rtl

关于swift - UILabel 中使用 NSAttributedString 的 HTML 文本(带表格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57406338/

相关文章:

ios - 无法在 tableView 中设置标签框 :cellForRowAtIndexPath: method

ios - 属性字符串不适用于标签

ios - 将 2 个 NSAttributedStrings 放在一个标签中

ios - Swift - 如何使用 XIB 文件创建自定义 viewForHeaderInSection?

ios - 为什么在 swift 中数组声明的末尾需要大括号?

swift - AdMob 需要很长时间才能生成

Swift tableview Cell label change Text by button 标签

ios - 点击单元格时更改表格单元格内 UILabel 的颜色

ios - UILabel 上的 UIImage

ios - 如何找到引号之间所有字符串的所有范围