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

前端 未结 13 1492
逝去的感伤
逝去的感伤 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:06

    I found the perfect solution to this problem in Apple's Text Layout Programming Guide. Here is the solution Apple provides:

    NSLayoutManager *layoutManager = [textView layoutManager];
    unsigned numberOfLines, index;
    unsigned numberOfGlyphs = [layoutManager numberOfGlyphs];
    NSRange lineRange;
    
    for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++){
        (void) [layoutManager lineFragmentRectForGlyphAtIndex:index effectiveRange:&lineRange];
        index = NSMaxRange(lineRange);
    }
    

    This could easily be written into an extension for UITextView, or as a standalone method taking in a UITextView object as a parameter

提交回复
热议问题