0

I want to add a string in the highlighted area in the textview, I mean by the highlighted area, where the blue line is located.

So once the user click on the button it adds a string where the "blue line" is located

I used stringByAppendingString but it adds the string after the word exists only

AFB
  • 550
  • 2
  • 8
  • 25
  • The text is UITextView is immutable, so you can't change the text on it. You have to enter a new text to display on it. – Himanshu Joshi Mar 05 '14 at 13:00
  • 1
    http://stackoverflow.com/questions/8846749/how-to-insert-text-at-any-cursor-position-in-uitextview – Pandey_Laxman Mar 05 '14 at 13:02
  • @HimanshuJoshi you can always set a placeholder text and then on the tap of a button, you can either clear the text field to enter new text or as per the question, place the cursor as per your needs – madLokesh Mar 05 '14 at 13:05
  • 1
    @kevinmartin check my answer. Is that something you are looking for ? – madLokesh Mar 05 '14 at 13:06
  • 1
    @madLokesh I am saying that you can't edit the text Of UItextField, instead you have to make a new string or just override the previous text to make the change – Himanshu Joshi Mar 05 '14 at 13:08
  • Did you read the documentation for NSString and for NSMutableString? And stringByAppendingString was the only method that you could find to get a modified string? Read the documentation again, carefully. These are your basic tools, you need to learn them. – gnasher729 Mar 27 '15 at 13:09

3 Answers3

3

You need to use the selectedRange to find out where the text cursor is. Then use replaceCharactersInRange:withString: or insertString:atIndex: to insert the new text into the original text. Then update the text into the view.

Wain
  • 118,658
  • 15
  • 128
  • 151
3
NSRange range = myTextView.selectedRange;  
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];  
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];  
myTextView.scrollEnabled = NO;  // turn off scrolling  

NSString * insertingString = [NSString stringWithFormat:@"your string value here"];

myTextView.text = [NSString stringWithFormat: @"%@%@%@",  
             firstHalfString,  
             insertingString,  
             secondHalfString];  
range.location += [insertingString length];  
 myTextView.selectedRange = range;  
 myTextView.scrollEnabled = YES; 
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
1

Even though its not clear what you are trying to achieve, it seems that you want the user to start editing the textfield from the position where text starts. In that case , you can refer following:

Hint 1

Set your view controller (or some other appropriate object) as the text field's delegate and implement the textFieldDidBeginEditing: method like this:

- (void)textFieldDidBeginEditing:(UITextField *)textField
 {
    UITextPosition *beginning = [textField beginningOfDocument];
    [textField setSelectedTextRange:[textField textRangeFromPosition:beginning
                                                      toPosition:beginning]];
 }

Note that setSelectedTextRange: is a protocol method of UITextInput (which UITextField implements), so you won't find it directly in the UITextField documentation.

Hint 2

self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];

Hint 3

finding-the-cursor-position-in-a-uitextfield/

madLokesh
  • 1,860
  • 23
  • 49