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 8866745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:52:12+00:00 2026-06-14T16:52:12+00:00

I’m trying to use the UITextField’s return key to insert a custom character. Here’s

  • 0

I’m trying to use the UITextField’s “return” key to insert a custom character. Here’s what my UITextFieldDelegate method looks like:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField insertText:@"¶"];
    return NO;
}

Unfortunately, this only works some of the time:

  • “one two|” –> move cursor –> “one| two” –> return –> “one¶| two” (OK)
  • “onetwo|” –> return –> “onetwo¶|” (OK)
  • “onetwo|” –> move cursor –> “one|two” –> return –> “onetwo¶|” (FAIL)

In the last case I would have expected “one¶|two”.

How do I ensure that the inserted text is always inserted at the cursor position?

Thanks.

  • 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-06-14T16:52:13+00:00Added an answer on June 14, 2026 at 4:52 pm

    The problem is when you tap the return key on the keyboard, the text field sets the selected range (the cursor position) to the end of its text before it sends you the textFieldShouldReturn: message.

    You need to keep track of the cursor position so you can restore it to its prior position. Let’s say you have a reference to the text field in a property:

    @interface ViewController () <UITextFieldDelegate>
    
    @property (strong, nonatomic) IBOutlet UITextField *textField;
    
    @end
    

    You’ll need an instance variable to hold the prior selected text range (from before the return key was tapped):

    @implementation ViewController {
        UITextRange *priorSelectedTextRange_;
    }
    

    Then you can write a method that saves the selected text range to the instance variable:

    - (void)saveTextFieldSelectedTextRange {
        priorSelectedTextRange_ = self.textField.selectedTextRange;
    }
    

    and in textFieldShouldReturn:, before you insert the pilcrow, you can change the selected text range back to its prior value:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        textField.selectedTextRange = priorSelectedTextRange_;
        [textField insertText:@"¶"];
        return NO;
    }
    

    But how can we make the system send the saveTextFieldSelectedTextRange message when we need it to?

    • The UITextFieldDelegate protocol doesn’t have messages for changes to the selected range.

    • UITextField doesn’t post any notifications for changes to the selected range.

    • The UITextInputDelegate protocol does have selectionWillChange: and selectionDidChange: messages, but the system sets the text field’s inputDelegate to its own UIKeyboardImpl object when the text field begins editing, so we can’t use the inputDelegate.

    • Key-value observing on the text field’s selectedTextRange property isn’t reliable. In my testing on the iOS 6.0 simulator, I don’t get a KVO message when I move the cursor from the middle to the end of the text by tapping the text field.

    The only way I can think of to reliably track changes to the text field’s selected range is by adding an observer to the run loop. On every pass through the event loop, the observer runs before event processing, so it can grab the current selected range before it changes.

    So we actually need another instance variable, to hold the reference to our run loop observer:

    @implementation ViewController {
        UITextRange *priorSelectedTextRange_;
        CFRunLoopObserverRef runLoopObserver_;
    }
    

    We create the observer in viewDidLoad:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createRunLoopObserver];
    }
    

    and we destroy it in both viewDidUnload and in dealloc:

    - (void)viewDidUnload {
        [super viewDidUnload];
        [self destroyRunLoopObserver];
    }
    
    - (void)dealloc {
        [self destroyRunLoopObserver];
    }
    

    To create the observer, we need a plain old C function for it to call. Here’s that function:

    static void runLoopObserverCallback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
        __unsafe_unretained ViewController *self = (__bridge ViewController *)info;
        [self saveTextFieldSelectedTextRange];
    }
    

    Now we can actually create the observer and register it with the main run loop:

    - (void)createRunLoopObserver {
        runLoopObserver_ = CFRunLoopObserverCreate(NULL, kCFRunLoopAfterWaiting, YES, 0, &runLoopObserverCallback, &(CFRunLoopObserverContext){
            .version = 0,
            .info = (__bridge void *)self,
            .retain = CFRetain,
            .release = CFRelease,
            .copyDescription = CFCopyDescription
        });
        CFRunLoopAddObserver(CFRunLoopGetMain(), runLoopObserver_, kCFRunLoopCommonModes);
    }
    

    and here’s how we actually deregister the observer and destroy it:

    - (void)destroyRunLoopObserver {
        if (runLoopObserver_) {
            CFRunLoopRemoveObserver(CFRunLoopGetMain(), runLoopObserver_, kCFRunLoopCommonModes);
            CFRelease(runLoopObserver_);
            runLoopObserver_ = NULL;
        }
    }
    

    This approach works in my testing on the iOS 6.0 simulator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.