Skip to content

Instantly share code, notes, and snippets.

@alexpersian
Created December 10, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpersian/17bc6093cc5e66c7210200cb343652b0 to your computer and use it in GitHub Desktop.
Save alexpersian/17bc6093cc5e66c7210200cb343652b0 to your computer and use it in GitHub Desktop.
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