2

I have been trying to solve this issue for a few days. I will appreciate any help. If it makes a difference to you, my app is free and used by a lot of people who have lost the ability to speak on their own.

The app is text to speech. I am implementing a feature where if they type text and then hit the Return key, the following will occur:

  • the text will automatically be spoken
  • the text will be saved to a temp location in case they have to repeat what they said
  • the screen will be cleared and an Undo button appears, so they can add the text back to the textView if needed (for repeating).

The problem I am having is when the Return key is pressed, the cursor moves down a line. I think it looks weird to have a clear textView with the cursor a line down (picture below). The rest works, even the Undo feature. I have tried many suggestions I have found here for moving the cursor programmatically and nothing seems to work.

Here is the code I use to do the previous steps. Any suggestions appreciated.

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [self speakText];
        self.clearedText = self.textView.text;
        self.undoReady = YES;
        [self.clearButton setTitle:@"Undo" forState:UIControlStateNormal];
        self.textView.text = @"";
        [WANT TO MOVE CURSOR TO BEGINNING POSITION];
    }
}

enter image description here

d.altman
  • 355
  • 4
  • 15

5 Answers5

4

This is working fine for me.

dispatch_async(dispatch_get_main_queue(), ^{
    textView.selectedRange = NSMakeRange(0, 0);
});

You can use this code whenever you want to set your textview position to start.

And change your range to (1,0) if you want your cursor to start after one letter.

Himanth
  • 2,381
  • 3
  • 28
  • 41
0

try with this-

textField.selectedTextRange = [textField
        textRangeFromPosition:textField.beginningOfDocument
        toPosition:textField.beginningOfDocument];
Return Zero
  • 424
  • 4
  • 15
0

use the delegate method of textfield

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {}

The string gives you the key-character which you pressed.

Inside the method check for carriage return/ enter.

if ([string isEqualToString:@"\n"]) { }

then inside "If statement" after your code clear the textfield and move cursor to beginning through below code.

//Your code
self.txtView.text = @"";
textField.selectedTextRange = [textField textRangeFromPosition:textField.beginningOfDocument toPosition:textField.beginningOfDocument];

Hope this will Help. Don't forget to accept the answer if it helps you.

Faisal
  • 162
  • 1
  • 12
0
1) First Add UITextViewDelegate -

@interface SecondViewController : UIViewController<UITextViewDelegate>

2) To move the Cursor to the beginning position, use the Delegate method of TextView -

- (void)textViewDidBeginEditing:(UITextView *)textView {
    dispatch_async(dispatch_get_main_queue(), ^{
        textView.selectedRange = NSMakeRange(0, 0);
    });
}
Megha_Singh
  • 100
  • 4
0

UITextField conforms to the UITextInput protocol, which provides methods that let you control the selected range

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.selectedTextRange = [textField
        textRangeFromPosition:textField.beginningOfDocument
        toPosition:textField.beginningOfDocument];
}
dmaulikr
  • 458
  • 5
  • 20