Swift 是否有方法使用与设置中不同的文本语言在UITextView/UILabel上进行正确的连字符?

Swift 是否有方法使用与设置中不同的文本语言在UITextView/UILabel上进行正确的连字符?,swift,hyphenation,Swift,Hyphenation,我的问题是,我的应用程序中有不同语言的文本块,在某些语言中(德语),单词可能太长,我正在使用连字号因子进行适当的布局。。。但正如文档中所说:“此属性通过检查首选语言中的第一项来检测用户选择的语言。”在我的例子中,设置中可能有英语,屏幕上可能有德语文本,因此断字的语法规则是错误的 是否有办法更改此属性的语言或对此进行一些修改?提前谢谢你 最终,弗兰克·劳希的这一扩展才是真正的帮助 添加了一些内容以检测字符串的主要语言,并限制应按字符数连字符的单词 import Foundation import

我的问题是,我的应用程序中有不同语言的文本块,在某些语言中(德语),单词可能太长,我正在使用连字号因子进行适当的布局。。。但正如文档中所说:“此属性通过检查首选语言中的第一项来检测用户选择的语言。”在我的例子中,设置中可能有英语,屏幕上可能有德语文本,因此断字的语法规则是错误的


是否有办法更改此属性的语言或对此进行一些修改?提前谢谢你

最终,弗兰克·劳希的这一扩展才是真正的帮助

添加了一些内容以检测字符串的主要语言,并限制应按字符数连字符的单词

import Foundation
import NaturalLanguage

extension String {

    func detectedLanguage(for string: String) -> String? {
        let recognizer = NLLanguageRecognizer()
        recognizer.processString(string)
        guard let languageCode = recognizer.dominantLanguage?.rawValue else { return nil }
        let detectedLanguage = Locale.current.localizedString(forIdentifier: languageCode)
        return detectedLanguage
    }

    func autoHyphenated() -> String {
        return self.hyphenated(languageCode: detectedLanguage(for: self) ?? "")
    }

    func hyphenated(languageCode: String) -> String {
        let locale = Locale(identifier: languageCode)
        return self.hyphenated(locale: locale)
    }

    func hyphenated(locale: Locale, wordMinimumLenght: Int = 13) -> String {
    guard CFStringIsHyphenationAvailableForLocale(locale as CFLocale) else {     return self }

        var s = self

        var words = s.components(separatedBy: " ")

        for index in 0..<words.count {
            if words[index].count > wordMinimumLenght && !words[index].contains("-") {
                let fullRange = CFRangeMake(0, words[index].utf16.count)
                var hyphenationLocations = [CFIndex]()
                for (i, _) in words[index].utf16.enumerated() {
                    let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(words[index] as CFString, i, fullRange, 0, locale as CFLocale, nil)
                    if hyphenationLocations.last != location {
                        hyphenationLocations.append(location)
                    }
                }
                for l in hyphenationLocations.reversed() {
                    guard l > 0 else { continue }
                    let strIndex = String.Index(utf16Offset: l, in: words[index])
                    words[index].insert("\u{00AD}", at: strIndex)
                }
            }
        }

        s = words.joined(separator: " ")

        return s
    }
}
<代码>导入基础 进口自然语言 扩展字符串{ func detectedLanguage(用于字符串:string)->string{ 让识别器=NLLanguageRecognizer() 识别器.processString(字符串) guard let languageCode=recognizer.dominantLanguage?.rawValue else{return nil} 让detectedLanguage=Locale.current.localizedString(forIdentifier:languageCode) 返回检测语言 } func autoHyphenated()->字符串{ 返回self.hyphenated(语言代码:detectedLanguage(for:self)??“”) } func连字符(语言代码:String)->String{ 让locale=locale(标识符:languageCode) 返回self.hyphenated(区域设置:区域设置) } func连字符(locale:locale,wordminimumlength:Int=13)->String{ guard CFStringIsHyphenationAvailableForLocale(区域设置为CFLocale)else{return self} var s=自我 var words=s.components(以:“”分隔) 对于0..WordMinimumLength&!Word[index]中的索引,包含(“-”){ 让fullRange=CFRangeMake(0,字[index].utf16.count) var hyphenationLocations=[CFIndex]() 对于字[index].utf16.enumerated()中的(i,u){ let location:CFIndex=cfStringGetHypenationLocationBeforeIndex(单词[index]作为CFString,i,fullRange,0,locale作为CFLocale,nil) 如果连字符位置.last!=位置{ 断字位置。追加(位置) } } 对于连字符位置中的l.reversed(){ 保护l>0其他{继续} 设strIndex=String.Index(utf16Offset:l,in:words[Index]) 单词[索引]。插入(“\u{00AD}”,位于:strIndex) } } } s=单词。已联接(分隔符:“”) 返回s } }
最终,弗兰克·劳希的这一扩展提供了真正的帮助

添加了一些内容以检测字符串的主要语言,并限制应按字符数连字符的单词

import Foundation
import NaturalLanguage

extension String {

    func detectedLanguage(for string: String) -> String? {
        let recognizer = NLLanguageRecognizer()
        recognizer.processString(string)
        guard let languageCode = recognizer.dominantLanguage?.rawValue else { return nil }
        let detectedLanguage = Locale.current.localizedString(forIdentifier: languageCode)
        return detectedLanguage
    }

    func autoHyphenated() -> String {
        return self.hyphenated(languageCode: detectedLanguage(for: self) ?? "")
    }

    func hyphenated(languageCode: String) -> String {
        let locale = Locale(identifier: languageCode)
        return self.hyphenated(locale: locale)
    }

    func hyphenated(locale: Locale, wordMinimumLenght: Int = 13) -> String {
    guard CFStringIsHyphenationAvailableForLocale(locale as CFLocale) else {     return self }

        var s = self

        var words = s.components(separatedBy: " ")

        for index in 0..<words.count {
            if words[index].count > wordMinimumLenght && !words[index].contains("-") {
                let fullRange = CFRangeMake(0, words[index].utf16.count)
                var hyphenationLocations = [CFIndex]()
                for (i, _) in words[index].utf16.enumerated() {
                    let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(words[index] as CFString, i, fullRange, 0, locale as CFLocale, nil)
                    if hyphenationLocations.last != location {
                        hyphenationLocations.append(location)
                    }
                }
                for l in hyphenationLocations.reversed() {
                    guard l > 0 else { continue }
                    let strIndex = String.Index(utf16Offset: l, in: words[index])
                    words[index].insert("\u{00AD}", at: strIndex)
                }
            }
        }

        s = words.joined(separator: " ")

        return s
    }
}
<代码>导入基础 进口自然语言 扩展字符串{ func detectedLanguage(用于字符串:string)->string{ 让识别器=NLLanguageRecognizer() 识别器.processString(字符串) guard let languageCode=recognizer.dominantLanguage?.rawValue else{return nil} 让detectedLanguage=Locale.current.localizedString(forIdentifier:languageCode) 返回检测语言 } func autoHyphenated()->字符串{ 返回self.hyphenated(语言代码:detectedLanguage(for:self)??“”) } func连字符(语言代码:String)->String{ 让locale=locale(标识符:languageCode) 返回self.hyphenated(区域设置:区域设置) } func连字符(locale:locale,wordminimumlength:Int=13)->String{ guard CFStringIsHyphenationAvailableForLocale(区域设置为CFLocale)else{return self} var s=自我 var words=s.components(以:“”分隔) 对于0..WordMinimumLength&!Word[index]中的索引,包含(“-”){ 让fullRange=CFRangeMake(0,字[index].utf16.count) var hyphenationLocations=[CFIndex]() 对于字[index].utf16.enumerated()中的(i,u){ let location:CFIndex=cfStringGetHypenationLocationBeforeIndex(单词[index]作为CFString,i,fullRange,0,locale作为CFLocale,nil) 如果连字符位置.last!=位置{ 断字位置。追加(位置) } } 对于连字符位置中的l.reversed(){ 保护l>0其他{继续} 设strIndex=String.Index(utf16Offset:l,in:words[Index]) 单词[索引]。插入(“\u{00AD}”,位于:strIndex) } } } s=单词。已联接(分隔符:“”) 返回s } }