Ios 更新NSLinguisticTagSchemeLanguage
你好,我现在正在做一个项目,我必须支持我的标签rtl文本 我有一个用swift 3编写的片段,但它不再适用于swift 4和5。我试图更新它,但我不知道如何更新它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
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
}
}
}