Last active
November 12, 2024 12:12
-
-
Save pgor/795dd3aaf027fcc058f243f025298228 to your computer and use it in GitHub Desktop.
Very basic implementation of enabling hyphenation for text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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