Created
December 10, 2019 15:58
-
-
Save alexpersian/17bc6093cc5e66c7210200cb343652b0 to your computer and use it in GitHub Desktop.
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
var isTruncating = false | |
// Make a range that encompases the entire text length. | |
let maxRange = NSRange(location: 0, length: Int.max) | |
// Use the layout manager to go through all of its line fragments within the range (entire text because of max range). | |
// More in-depth information is available within the NSLayoutManager docs. | |
layoutManager.enumerateLineFragments(forGlyphRange: maxRange) { _, _, _, glyphRange, stop in | |
// Ask the layout manager to find the range of the truncation glyph (...) within this line fragment | |
let truncatedRange = self.layoutManager.truncatedGlyphRange(inLineFragmentForGlyphAt: glyphRange.lowerBound) | |
// If the truncatedRange has a valid location that means the layout manager has detected the truncation glyph. | |
if truncatedRange.location != NSNotFound { | |
isTruncating = true | |
// NSLayoutManager uses a point to a boolean to know whether it should continue traversal. | |
// Changing `stop` to point to a value of `true` halts the operation. | |
stop.pointee = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment