Created
July 24, 2024 00:02
-
-
Save brownsoo/3cea9b1aaf83d2f852a2a746a44700d6 to your computer and use it in GitHub Desktop.
UITextView 라인수 계산
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 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