Есть ли готовая библиотека для переноса слов в тексте. Например, я загружаю текст в TextView то в конце строки если слово не помещается оно переносится целиком на новую строку, а хотелось бы разделить слово и поставить знак переноса. Может у кого была такая задача, подскажите как решали или поделитесь ссылками, заранее всем спасибо.
Перенос слов в тексте
NickNick
#2
нашел решение может кому пригодиться
extension String {
func russianHyphenated() -> String {
return hyphenated(locale: Locale(identifier: "ru_Ru"))
}
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.Index(utf16Offset: l, in: s)
// insert soft hyphen:
s.insert("\u{00AD}", at: strIndex)
// or insert a regular hyphen to debug:
//s.insert("-", at: strIndex)
}
return s
}
}