Counting the number of lines in a UITextView, lines wrapped by frame size

前端 未结 13 1496
逝去的感伤
逝去的感伤 2020-11-27 03:42

I wanted to know when a text is wrapped by the frame of the text view is there any delimiter with which we can identify whether the text is wrapped or not.

For insta

13条回答
  •  鱼传尺愫
    2020-11-27 04:02

    Swift 4 version of Luke Chase's answer

    let numberOfGlyphs = textView.layoutManager.numberOfGlyphs
    var index = 0, numberOfLines = 0
    var lineRange = NSRange(location: NSNotFound, length: 0)
    
    while index < numberOfGlyphs {
      textView.layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
      index = NSMaxRange(lineRange)
      numberOfLines += 1
    }
    

提交回复
热议问题