Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4120780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:59+00:00 2026-05-20T23:15:59+00:00

I use the following code to simulate a key press in a UITextView (and

  • 0

I use the following code to simulate a key press in a UITextView (and also to set the scroll to the insertion position):

NSRange selectedRange = textview.selectedRange;

NSString *currentText = textview.text;

NSString *yourString = @"x";

NSString *firstPart = [currentText substringToIndex: selectedRange.location];
NSString *lastPart = [currentText substringFromIndex: selectedRange.location];

NSString *modifiedText = [firstPart stringByAppendingFormat:@"%@%@", yourString, lastPart];

textview.text = modifiedText;

NSInteger loc = selectedRange.location + 1;
NSInteger len = textview.selectedRange.length;

NSRange newRange = NSMakeRange(loc, len);
textview.selectedRange = newRange;

As you can see, i divide the textview.text, i insert the @”x” in the cursor position and i modify the whole text. This works great, as a matter of fact, unless the length of the text file is big. And this sounds logical, given that i divide the whole thing in parts and so with every simulated key.

So with a small text file i have no problem whatsoever, but with a big one i can see a considerable lag.

Is there any way to do this with a better performance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:16:00+00:00Added an answer on May 20, 2026 at 11:16 pm

    Instead of rewriting the whole text at each keystroke, you should use the
    -(void)insertText:(NSString*)text method

    Sample code from here:

    @interface UIResponder(UIResponderInsertTextAdditions)
    - (void) insertText: (NSString*) text;
    @end
    
    @implementation UIResponder(UIResponderInsertTextAdditions)
    
    - (void) insertText: (NSString*) text
    {
        // Get a refererence to the system pasteboard because that's
        // the only one @selector(paste:) will use.
        UIPasteboard* generalPasteboard = [UIPasteboard generalPasteboard];
    
        // Save a copy of the system pasteboard's items
        // so we can restore them later.
        NSArray* items = [generalPasteboard.items copy];
    
        // Set the contents of the system pasteboard
        // to the text we wish to insert.
        generalPasteboard.string = text;
    
        // Tell this responder to paste the contents of the
        // system pasteboard at the current cursor location.
        [self paste: self];
    
        // Restore the system pasteboard to its original items.
        generalPasteboard.items = items;
    
        // Free the items array we copied earlier.
        [items release];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use following code to retrieve character index by position in
I am trying to use the following code to simulate a simple text-loading animation:
I have the following code I use to simulate a live data feed which
I use following code for guarantee startTime variable set once only: public class Processor
I use following code: Create a retry policy, when error, retry after 1 second,
I just use following code to add an image to my project, var paper
I use VS2010, C# to develop Silverlight 4 app, I use following code in
I have an array of objects, and i use following code to get in
I use the following code to crop an image according to a rectangular selection
I use the following code to copy a particular directory and its contents to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.