UITextField highlight all text
Asked Answered
B

4

5

I have searched through stackoverflow for a similar problem and found recommended solutions that worked for others but none of them worked for me so I'm beginning to suspect that it's something I haven't done properly.

It's very simple. All I want to have is that when the user taps on the uitextfield, the entire text within uitextfield get selected. The user can then proceed to delete it all, tap once and append from that point onwards or start typing to overwrite everything.

I have an action from the uitextfield and here's the snippet.

- (IBAction)didBeginEditDescription:(id)sender
{
    NSLog(@"Description began edit.");

    [self.txtfield selectall:self];
}

I know the method is called (evident by the NSLog). However, nothing happens and the cursor remains to be at the last position of the text. I have UITextFieldDelegate already so not sure what else I should look at?

FYI, this is being done to Xcode 5.0 and trying to develop for iOS 7.

Is there anything obvious that I'm missing?

Booted answered 2/10, 2013 at 10:0 Comment(0)
I
9

I think you want to highlight all the text of selected UITextField. In that case, first you should identify which UITextField called that method (-didBeginEditDescription) and then you can call -selectAll for that particular UITextField.

Sample Code :

- (IBAction)didBeginEditDescription:(id)sender
{
    NSLog(@"Description began edit.");
    UITextField *txtFld = ((UITextField *)sender);
    [txtFld selectAll:self];
}

Update :

-selectAll should work. I already implemented and it is working very well for me. Note that, you have written -selectall instead of -selectAll. You can also try like this :

[txtFld setSelectedTextRange:[txtFld textRangeFromPosition:txtFld.beginningOfDocument toPosition:txtFld.endOfDocument]]; 
Impolicy answered 2/10, 2013 at 10:32 Comment(3)
I just tried that and it didn't make any difference. It's very strange but I appreciate the response.Booted
I copied your snippet so the spelling mistake was corrected. I have no doubt that is probably the right code. It must be something else I'm not doing. The alternative you have suggested also doesn't give me the right result. Is there anything else that I should do? Perhaps on the IB or elsewhere in the code?Booted
@Booted : I can advice you the changes that I can see in the code. I have seen your above code and I told you the required changes. You have to check your code line by line using breakpoints. Only I can tell you is that "Good Luck". :)Impolicy
J
4

Xamarin iOS:

nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                {
                   nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
                };

Xamarin.Forms custom renderer for iOS:

using System;
using CoreText;
using ObjCRuntime;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;


[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace Hello.iOS
{    
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);    
            if (Control != null)
            {
                var nativeTextField = (UITextField)Control;    
                nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                {
                   nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
                };                           
            }
        }

    }

Xamarin.Forms custom renderer for Android:

  protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {


            EditText editText = null;

            for (int i = 0; i < ChildCount; ++i)
            {
                Android.Views.View view = (Android.Views.View)GetChildAt(i);
                if (view is EditText) editText = (EditText)view;
            }

            editText?.SetSelectAllOnFocus(true);            

        }
    }
Jauregui answered 29/9, 2017 at 9:34 Comment(0)
M
1

I found it was necessary to put call selectAll:self upon TouchDown: rather than editDidBegin:

Attach Touch Down to - (IBAction)didBeginEditDescription:(id)sender in InterfaceBuilder.

Milepost answered 18/2, 2016 at 13:58 Comment(1)
Yes, I found the same thing. Note that it adds the select handles, which are annoying for this purpose.Buckra
H
1

For Xamarin.Forms, create a custom renderer with this method:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);
    if (e.NewElement == null) return;
    Control.EditingDidBegin += (sender, e) => Control.PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
}
Hydrophobic answered 2/2, 2018 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.