Skip to content

Instantly share code, notes, and snippets.

@brownsoo
Created July 24, 2024 00:02
Show Gist options
  • Save brownsoo/3cea9b1aaf83d2f852a2a746a44700d6 to your computer and use it in GitHub Desktop.
Save brownsoo/3cea9b1aaf83d2f852a2a746a44700d6 to your computer and use it in GitHub Desktop.
UITextView 라인수 계산
import Foundation
import UIKit
public extension UITextView {
func numberOfLines() -> Int {
let numberOfGlyphs = base.layoutManager.numberOfGlyphs
var index = 0, lineCount = 0
var lineRange = NSRange(location: NSNotFound, length: 0)
while index < numberOfGlyphs {
base.layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
index = NSMaxRange(lineRange)
lineCount += 1
}
return lineCount
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment