Skip to content

Instantly share code, notes, and snippets.

@pgor
Last active November 12, 2024 12:12
Show Gist options
  • Save pgor/795dd3aaf027fcc058f243f025298228 to your computer and use it in GitHub Desktop.
Save pgor/795dd3aaf027fcc058f243f025298228 to your computer and use it in GitHub Desktop.
Very basic implementation of enabling hyphenation for text.
import UIKit
public extension String {
/// Transforms this string into an NSAttributedString that has hyphenation enabled.
/// Especially at larger preferredContentSizes, regular word wrapping can lead to really
/// choppy line breaks. Hyphenating allows fuller use of the line whenever possible,
/// resulting in more text on the screen.
/// - Returns: An NSAttributedString version of this String with hyphenation enabled
func withHyphenationEnabled() -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1.0
return NSAttributedString(string: self, attributes: [.paragraphStyle: paragraphStyle])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment