Bou*_*rne 7 iphone uitextview ipad
我有一个可编辑的UITextView.现在我有一个要求,要求我找到(当我一直打字时)关于下一行何时开始(可能是因为我点击了返回键,或者是自动wordwrap换行符).是否有任何通知可以获取以确定下一行何时开始输入?
我试图寻找解决方案,找出文本视图中的光标位置,但使用selectedRange和位置属性来找出它并没有帮助我.位置值和新行之间根本没有相关性.键入时位置值会不断增加.有任何想法吗?
谢谢!
Kin*_*iss 11
只要在textView中输入新文本,就会调用以下委托.
设置UITextView的委托,然后编码如下
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if ( [text isEqualToString:@"\n"] ) {
        //Do whatever you want
    }
    return YES;
}
对于Swift使用此
previousRect = CGRectZero
 func textViewDidChange(textView: UITextView) {
        var pos = textView.endOfDocument
        var currentRect = textView.caretRectForPosition(pos)
        if(currentRect.origin.y > previousRect?.origin.y){
            //new line reached, write your code
        }
        previousRect = currentRect
    }
对于目标C
CGRect previousRect = CGRectZero;
- (void)textViewDidChange:(UITextView *)textView{
    UITextPosition* pos = textView.endOfDocument;
    CGRect currentRect = [textView caretRectForPosition:pos];
    if (currentRect.origin.y > previousRect.origin.y){
            //new line reached, write your code
        }
    previousRect = currentRect;
}
将检测从任何内容到点击“返回”、退格以减少行数、键入直到行尾并发生自动换行等的行更改。(*注意:必须调整字体大小的变量,我建议不要使用硬编码数字,如下面的示例所示)。
previousNumberOfLines = ((hiddenText.contentSize.height-37+21)/(21));//numbers will change according to font size
NSLog(@"%i", previousNumberOfLines);
| 归档时间: | 
 | 
| 查看次数: | 9634 次 | 
| 最近记录: |