How to insert text into UITextView from code?

Hi,

I am trying to insert text into a cursor position in a UITextView from code, but it doesn't work as I want.

Here is the code I wrote.

-----
- (void) insertString: (NSString *) insertingString
intoTextView: (UITextView *) textView
{
NSString * firstHalfString = [textView.text substringToIndex: textView.selectedRange.location];
NSString * secondHalfString = [textView.text substringFromIndex: textView.selectedRange.location];

textView.text = [NSString stringWithFormat: @"%@%@%@",
firstHalfString,
insertingString,
secondHalfString];
}
-----

This inserts insertingString, but after this method is called, the UITextView always scrolls to the end of the text. But I want the UITextView remain the same position and don't scroll.

What is the best solution to this?

Mac Pro, MacBook Air, iPhone 3G, Mac OS X (10.5.4)

Posted on Mar 13, 2009 7:39 PM

Reply
3 replies

Mar 14, 2009 3:41 AM in response to chataka

I can only provide part of what you want. Here's some code that will scroll to the insertion point:

- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView {
NSRange range = textView.selectedRange;
NSString * firstHalfString = [textView.text substringToIndex:range.location];
NSString * secondHalfString = [textView.text substringFromIndex: range.location];
NSLog(@"insertString: location=%d", range.location);
textView.text = [NSString stringWithFormat: @"%@%@%@",
firstHalfString,
insertingString,
secondHalfString];
NSValue *value = [[NSValue valueWithRange:range] retain];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(doScroll:) userInfo:value repeats:NO];
NSLog(@"insertString: timer=%@", timer);
}
- (void)doScroll:(NSTimer*)theTimer {
NSValue *value = [theTimer userInfo];
NSRange range = [value rangeValue];
NSLog(@"doScroll: location=%d", range.location);
[theTextView scrollRangeToVisible:range];
[value release];
[theTimer invalidate];
}

The timer is necessary because scrollRangeToVisible: doesn't do what we want if it's called within the same event loop as [textView setText:]. Also I guess you know that the above can't be called unless textView is editing; i.e. I think both the keyboard and the caret must be visible else textView.selectedRange throws an exception.

I realize this isn't all of what you want. When we change the text property, the caret wants to go to the end of the new text and the view then wants to scroll until the caret is visible. I don't see any way to move the caret programmatically from outside the class.

So it looks like we'd need to subclass UITextView to be able to insert only the desired text. The caret would then travel to where we want and no scrolling would occur except to keep the caret visible.

Sure hope that helps. Best I can do for now.

Aug 5, 2009 2:20 AM in response to RayNewbie

Hi RayNewbie,

I have read ur comments for this issue it is good....i want the clicked point in textview to move up...
Now i am using custom textview with editable as no. Assume that i have some static long content.Using my text view touch ended method i am getting touch point and move the touch point to visible using scrollrecttovisible method. I am making text view as editable in touches ended method.But the problem is the cursor always blinks at the end.I want the cursor at the clicked point..

Please help me out..

Message was edited by: Sakthivel

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to insert text into UITextView from code?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.