如何通过在UITextView iOS中点击返回键来创建新行 [英] How to create a new line by tapping the return key in UITextView ios

查看:166
本文介绍了如何通过在UITextView iOS中点击返回键来创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚上iPhone.我在应用程序中添加了一个使用XIB的UITextView,现在点击完成键后,它只是在为键盘重新签名.但是,我想通过点击它来创建新行,这意味着它应该转到下一段.请帮助我实现我的输出.这是我的代码:

I am newly to iPhone. I have added one UITextView using XIB in my application, now after tapping on done key it's simply resigning the keyboard. But, I want to create a new line by tapping it, means it should go to the next paragraph. Please help me to achieve my output. Here is my code :

- (void)textViewDidEndEditing:(UITextView *)textView1
{
    if (![textView1 hasText]) {
        [textView1 addSubview:placeholderLabel];
    }
    else{
        NSLog(@"done button clicked!!");
    }

    CGRect rect;

    if(screenBounds.size.height == 568)//iPhone5
    {
        rect = CGRectMake(5, 76, 310, 491);
    }
    else{
        if(_kisiOS7){
            rect = CGRectMake(5, 76, 310, 404);
        }
        else{
            rect = CGRectMake(0, 44, 320, 436);
        }
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    txtView.frame = rect;

    [UIView commitAnimations];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.length == 0) {
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            return NO;
        }
    }

    return YES;
}

谢谢.

推荐答案

您应实现具有返回值NO的委托方法(因此,在点击"return"或"done"时,它不会关闭键盘).

You should implement delegate method with returning value NO (so on tapping "return" or "done" it will not close keyboard).

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return NO;
}

并删除或更改下一行代码的逻辑:

and remove or change logic of next lines of code:

if (range.length == 0) {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
}

因此,在textViewShouldEndEditing中,您可以确定/计算何时需要关闭键盘的情况(如果要关闭,请返回YES,否则请返回NO)

So in textViewShouldEndEditing you can determine/calculate situations when you need to close keyboard (if you want to close - return YES, otherwise return - NO)

您还可以更改

  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)范围replaceText:(NSString *)text {
  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if (range.length == 0) {
    if ([text isEqualToString:@"\n"]) {
        textView.text = [NSString stringWithFormat:@"%@\n\t",textView.text];
        return NO;
    }
}

在这种情况下,用户将点击键盘上的操作按钮(例如返回"). Textview将在文本中插入新行和其他标签.

In this case when user will tap on action button on keyboard (like "return"). Textview will insert new line and additional tab in text.

我希望它能对您有所帮助.

I hope it will help you.

这篇关于如何通过在UITextView iOS中点击返回键来创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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