【问题标题】:In UITextView, how to get the point that next input will begin another line在 UITextView 中,如何获取下一个输入将开始另一行的点
【发布时间】:2013-08-26 12:25:47
【问题描述】:

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

我怎样才能得到这个?

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

----经过长时间的研究,我发现下面是可以的,但是看起来有点复杂。

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

【问题讨论】:

    标签: ios uitextview


    【解决方案1】:

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

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

    【讨论】:

    • 我知道我可以获得行数,但是,我怎样才能正确地知道下一个输入文本将开始另一个新行的时刻/点。
    猜你喜欢
    • 2016-10-30
    • 2020-04-02
    • 1970-01-01
    • 2017-11-25
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    相关资源
    最近更新 更多