Я использую этот код для вставки дефисов в строку для лучшего переноса слов.С помощью Swift 5 я получаю информацию, что
String.UTF16View.Index (encodedOffset: l)
будет считаться устаревшим.Но я не могу понять, правильные параметры.Есть идеи?
import Foundation
extension String {
func hyphenated(languageCode: String) -> String {
let locale = Locale(identifier: languageCode)
return self.hyphenated(locale: locale)
}
func hyphenated(locale: Locale) -> String {
guard CFStringIsHyphenationAvailableForLocale(locale as CFLocale) else { return self }
var s = self
let fullRange = CFRangeMake(0, s.utf16.count)
var hyphenationLocations = [CFIndex]()
for (i, _) in s.utf16.enumerated() {
let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(s 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.UTF16View.Index(encodedOffset: l)
// insert soft hyphen:
s.insert("\u{00AD}", at: strIndex)
// or insert a regular hyphen to debug:
// s.insert("-", at: strIndex)
}
return s
}
}