在 UITextView 中,如何获取下一个输入将开始另一行的点 [英] In UITextView, how to get the point that next input will begin another line

查看:19
本文介绍了在 UITextView 中,如何获取下一个输入将开始另一行的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iOS 中使用 UITextView,并希望获得下一个输入文本将开始另一个新行的时刻/点.

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

我怎样才能得到这个?

-----已编辑-----

-----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;
}

推荐答案

在 UITextView 的这个委托方法中,这段代码会在每次文本域改变时统计行数:

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;
}

这篇关于在 UITextView 中,如何获取下一个输入将开始另一行的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆