3

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

Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2786
  • 656
  • 4
  • 13
  • 35
  • @mins Thanks for the response, I have seen that link but it was not clear for me that either text view keyboard will have option Alt + Enter for new line or I need to set anywhere. As in my simulator keyboard, I am not getting such keys. please help me sir. – user2786 Apr 12 '15 at 09:35
  • You need to discard the `\n` when entered by the user, and continue reading the keyboard for the next input. I'm not an iOS expert, but the two resources may hopefully provide the details you are looking for: [here](http://iphonedevsdk.com/forum/iphone-sdk-development/90565-how-can-a-user-enter-multiple-lines-of-text-in-my-editable-uitextview.html), and [here](http://stackoverflow.com/questions/21077842/uitextview-adding-new-line-unintentionally/21078008#21078008). – mins Apr 12 '15 at 09:52

4 Answers4

8

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

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

You can also change logic of

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

to

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

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.

sfirite
  • 161
  • 4
0

The culprit is [textView resignFirstResponder];.

Do you really need - textView:shouldChangeTextInRange:replacementText:? If not, just delete the whole method, going to next line is the default behaviour of pressing the "done"(it's "return" by default) key and it has been modified by [textView resignFirstResponder]; in - textView:shouldChangeTextInRange:replacementText:. If you do, at least you need to delete [textView resignFirstResponder]; to avoid your description.

fujianjin6471
  • 5,168
  • 1
  • 36
  • 32
0

Insert new line by tapping the return key in UITextView ios

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if textView == textViewForChat
    {


        if (text == "\n")
        {
            textView.text = textView.text + "\n"
            //textView.resignFirstResponder()

         }

     }
return true
}
RJ raj
  • 17
  • 5
-1

swift 3 , 4

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == text.trimmingCharacters(in: .newlines) {
            return true;
        }else{
            textView.resignFirstResponder()
            return false
        }
    }
Community
  • 1
  • 1
Amr Angry
  • 3,711
  • 1
  • 45
  • 37