Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更新NSLinguisticTagSchemeLanguage_Ios_Swift_Textview_Uilabel_Swift5 - Fatal编程技术网

Ios 更新NSLinguisticTagSchemeLanguage

Ios 更新NSLinguisticTagSchemeLanguage,ios,swift,textview,uilabel,swift5,Ios,Swift,Textview,Uilabel,Swift5,你好,我现在正在做一个项目,我必须支持我的标签rtl文本 我有一个用swift 3编写的片段,但它不再适用于swift 4和5。我试图更新它,但我不知道如何更新它 extension UILabel { func decideTextDirection () { let tagScheme = [NSLinguisticTagSchemeLanguage] let tagger = NSLinguisticTagger(tagSchemes: tagScheme, opti

你好,我现在正在做一个项目,我必须支持我的标签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()

我把你的扩展扔到操场上,把编译器的错误敲定了。以下是我的想法:

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