更新 NSLinguisticTagSchemeLanguage

Updating NSLinguisticTagSchemeLanguage

你好,我现在正在做一个项目,我必须在我的标签中支持 rtl 文本

我有这个写在 swift 3 中的片段,但它不再适用于 swift 4 和 5。我尝试更新它,但我不知道如何更新它

extension UILabel {
func decideTextDirection () {
    let tagScheme = [NSLinguisticTagSchemeLanguage]
    let tagger    = NSLinguisticTagger(tagSchemes: tagScheme, options: 0)
    tagger.string = self.text
    let lang      = tagger.tag(at: 0, scheme: NSLinguisticTagSchemeLanguage,
                                      tokenRange: nil, sentenceRange: nil)

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

要使用此功能,请在您的标签中添加以下内容:

detailLabel.text = details[0]

detailLabel.decideTextDirection()

我把你的扩展放到了 Playground 中并解决了编译器错误。这是我想出的:

extension UILabel {
    func decideTextDirection () {
        let tagScheme = [NSLinguisticTagScheme.language]
        let tagger = NSLinguisticTagger(tagSchemes: tagScheme, options: 0)
        tagger.string = self.text
        let lang = tagger.tag(at: 0, scheme: NSLinguisticTagScheme.language,
                                   tokenRange: nil, sentenceRange: nil)

        if let _ = lang?.rawValue.contains("ar") {
            self.textAlignment = NSTextAlignment.right
        } else {
            self.textAlignment = NSTextAlignment.left
        }
    }
}