Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- extension String {
- func hungarianHyphenated() -> String {
- return hyphenated(locale: Locale(identifier: "hu_HU"))
- }
- 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.indices.reversed() {
- let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(s as CFString, i.utf16Offset(in: self), fullRange, 0, locale as CFLocale, nil)
- if location < 0 {
- return s
- }
- if hyphenationLocations.last != location {
- hyphenationLocations.append(location)
- let strIndex = String.Index(utf16Offset: location, in: s)
- s.insert("\u{00AD}", at: strIndex)
- }
- }
- return s
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement