Skip to content

Instantly share code, notes, and snippets.

@KaQuMiQ
Created March 19, 2020 18:46
Show Gist options
  • Save KaQuMiQ/3985b6ecf41f48bc2d1575f37d6a2221 to your computer and use it in GitHub Desktop.
Save KaQuMiQ/3985b6ecf41f48bc2d1575f37d6a2221 to your computer and use it in GitHub Desktop.
Number of text lines in UITextView
extension NSLayoutManager {
var numberOfLines: Int {
var linesCount: Int = 0
var idx: Int = 0
let lineRange: NSRangePointer = .allocate(capacity: 1)
while idx < numberOfGlyphs {
lineFragmentRect(forGlyphAt: idx, effectiveRange: lineRange)
idx = NSMaxRange(lineRange.pointee)
linesCount += 1
}
lineRange.deallocate()
return linesCount
}
}
extension UITextView {
var numberOfLines: Int {
layoutManager.numberOfLines
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment