Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import Foundation
  2. extension String {
  3.  
  4. func hungarianHyphenated() -> String {
  5. return hyphenated(locale: Locale(identifier: "hu_HU"))
  6. }
  7.  
  8. func hyphenated(languageCode: String) -> String {
  9. let locale = Locale(identifier: languageCode)
  10. return self.hyphenated(locale: locale)
  11. }
  12.  
  13. func hyphenated(locale: Locale) -> String {
  14. guard CFStringIsHyphenationAvailableForLocale(locale as CFLocale) else { return self }
  15.  
  16. var s = self
  17.  
  18. let fullRange = CFRangeMake(0, s.utf16.count)
  19. var hyphenationLocations = [CFIndex]()
  20.  
  21. for i in s.utf16.indices.reversed() {
  22. let location: CFIndex = CFStringGetHyphenationLocationBeforeIndex(s as CFString, i.utf16Offset(in: self), fullRange, 0, locale as CFLocale, nil)
  23. if location < 0 {
  24. return s
  25. }
  26. if hyphenationLocations.last != location {
  27. hyphenationLocations.append(location)
  28. let strIndex = String.Index(utf16Offset: location, in: s)
  29. s.insert("\u{00AD}", at: strIndex)
  30. }
  31. }
  32.  
  33. return s
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement