How to Read Number of lines in UITextView

前端 未结 4 2095
野性不改
野性不改 2020-12-14 16:57

I am using UITextView In my View , I have requirement to count number of line contained by textview am using following function to read \'\\n\' . H

4条回答
  •  天涯浪人
    2020-12-14 17:33

    extension NSLayoutManager {
        var numberOfLines: Int {
            guard let textStorage = textStorage else { return 0 }
    
            var count = 0
            enumerateLineFragments(forGlyphRange: NSMakeRange(0, numberOfGlyphs)) { _, _, _, _, _ in
                count += 1
            }
            return count
        }
    }
    

    Get number of lines in textView:

    let numberOfLines = textView.layoutManager.numberOfLines
    

提交回复
热议问题