1

I'm using UITextView in iOS, and would like to get just the moment/point that next input text will begin another new line.

How can i get this?

-----Editted-------

----After a long time research, i found the below is OK, but it seems a little complicated.

//limit text within text box
NSString *originalStr = textView.text;
textView.text = [textView.text stringByAppendingString:text];
CGFloat lineHeight = textView.font.lineHeight;
int currentLines = textView.contentSize.height / lineHeight;
int maxLines = textView.frame.size.height/lineHeight;
if (currentLines > maxLines) {
    NSString * firstHalfString = [originalStr substringToIndex:range.location];
    NSString * secondHalfString = [originalStr substringFromIndex: range.location];
    textView.text = [NSString stringWithFormat: @"%@%@%@",
                     firstHalfString,
                     text,
                     secondHalfString];
    int timeout = 0;
    while (true) {
        timeout =50;
        textView.text = [textView.text substringToIndex:(textView.text.length -1)];
        int newCurrentLines = textView.contentSize.height / lineHeight;
        if ((newCurrentLines == maxLines) || (timeout > 50))
            break;
    }

    textView.selectedRange = range;
    return false;
} else {
    textView.text = originalStr;
    textView.selectedRange = range;
    return true;
}
liangwang
  • 707
  • 1
  • 6
  • 15

1 Answers1

0

In this delegate method of the UITextView, this code will count the number of lines each time the text field is changed :

-(void)textViewDidChange:(UITextView *)textView
{
    UIFont *font = [UIFont boldSystemFontOfSize:11.0];
    CGSize size = [string sizeWithFont:font 
                     constrainedToSize:myUITextView.frame.size 
                         lineBreakMode:UILineBreakModeWordWrap]; // default mode
    float numberOfLines = size.height / font.lineHeight;
}
zbMax
  • 2,756
  • 3
  • 21
  • 42
  • I know I can get number of lines, however, how can I get right the moment/point that next input text will begin another new line. – liangwang Aug 26 '13 at 21:03