7

In iOS 6 I used this code to get the number of lines in the UITextView:

int numLines = textView.contentSize.height / textView.font.lineHeight;

But this is not working in iOS 7. How do I get the total number of lines of the UITextView in iOS 7?

mattsven
  • 22,305
  • 11
  • 68
  • 104
hazem
  • 289
  • 4
  • 14
  • have a look here : http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative/19067476#19067476 – Jordan Montel Oct 01 '13 at 18:17
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5837348/counting-the-number-of-lines-in-a-textview-lines-wrapped-by-frame-size – Szu Jun 11 '14 at 15:19
  • Possible duplicate of [Counting the number of lines in a UITextView, lines wrapped by frame size](https://stackoverflow.com/questions/5837348/counting-the-number-of-lines-in-a-uitextview-lines-wrapped-by-frame-size) – CristinaTheDev Jul 25 '17 at 13:24
  • Possible duplicate of [How to find UITextView number of lines](https://stackoverflow.com/questions/7320361/how-to-find-uitextview-number-of-lines) – iWheelBuy Dec 27 '17 at 07:35

3 Answers3

4

You can use something like this. It's low performance but this is what I could figure till now.

NSString *string=textView3.text;
NSArray *array=[string componentsSeparatedByString:@"\n"];
NSLog(@"%d",array.count);
hgwhittle
  • 9,316
  • 6
  • 48
  • 60
4

You can do this with the UITextInputTokenizer/UITextInput protocol methods of UITextView.

id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextPosition *pos = textView.endOfDocument; int lines = 0;

while (true){
    UITextPosition *lineEnd = [tokenizer positionFromPosition:pos toBoundary:UITextGranularityLine inDirection:UITextStorageDirectionBackward];

    if([textView comparePosition:pos toPosition:lineEnd] == NSOrderedSame){
        pos = [tokenizer positionFromPosition:lineEnd toBoundary:UITextGranularityCharacter inDirection:UITextStorageDirectionBackward];

        if([textView comparePosition:pos toPosition:lineEnd] == NSOrderedSame) break;

        continue;
    }

    lines++; pos = lineEnd;
}

//lines--; // Compensation for extra line calculated (??)

(GIST)

mattsven
  • 22,305
  • 11
  • 68
  • 104
0

you can pass the String and textView to this method and it will return the current number rows in this textview even if the user pressed new line:

-(int)numberOfRows:(NSString *)string inView:(UITextView*)txtView
{

CGSize maximumLabelSize = CGSizeMake(MAXFLOAT , MAXFLOAT);

NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin;

NSString *new = [string stringByReplacingOccurrencesOfString: @"\n" withString:@" "];
NSDictionary *attr = @{NSFontAttributeName: [UIFont fontWithName:urFont size:urSize]};
CGRect bounds = [new boundingRectWithSize:maximumLabelSize
                                          options:options
                                       attributes:attr
                                          context:nil];

CGSize viewSize = bounds.size;


int row = floor(txtView.contentSize.height / viewSize.height ) ;


return row;
}
Ayman Ibrahim
  • 1,359
  • 15
  • 24