Skip to content

Instantly share code, notes, and snippets.

@frankrausch
Last active June 15, 2023 16:45
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frankrausch/0d2c91fad5e417a84aaa43bfb9c9aec8 to your computer and use it in GitHub Desktop.
Save frankrausch/0d2c91fad5e417a84aaa43bfb9c9aec8 to your computer and use it in GitHub Desktop.
Returns a String with soft hyphens (U+00AD)
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
}
}
// print("""
// Here’s to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes…the ones who see things differently—they’re not fond of rules…You can quote them, disagree with them, glorify or vilify them, but the only thing you can’t do is ignore them because they change things…they push the human race forward, and while some may see them as the crazy ones, we see genius, because the ones who are crazy enough to think that they can change the world, are the ones who do.
// """.hyphenated(languageCode: "en"))
@frankrausch
Copy link
Author

Loosely based on this Objective-C implementation: https://stackoverflow.com/a/19858601

@ogezue
Copy link

ogezue commented Mar 27, 2019

String.UTF16View.Index(encodedOffset: l) is deprecated in Swift 5
use let strIndex = String.Index(utf16Offset: l, in: s) instead

@gezamiklo
Copy link

gezamiklo commented Apr 1, 2019

A one-pass solution for Swift 4.2

On iOS 12.2 the previous solution made some accented characters appear really awful.

`

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
}

}

`

@acotilla91
Copy link

Any idea how to make this work with words like "lady" and "project"? They both have 2 syllables but this solution does not seem to recognize that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment