UITextField selects all text

I searched stackoverflow for a similar problem and found recommended solutions that worked for others, but none of them worked for me, so I'm starting to suspect that this is something that I did not do right.

It is very simple. All I want to have is that when the user clicks on the uitext field, all the text within the uitextfield is selected. Then the user can continue the deletion, click once and add from now on or start typing to rewrite everything.

I have an action from a uitext field and here is a snippet.

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

    [self.txtfield selectall:self];
}

I know the method is called (obviously NSLog). However, nothing happens, and the cursor remains at the last position of the text. My UITextFieldDelegate is not so sure anymore, what else should I look at?

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

Is there something obvious that I'm missing?

+4
source share
4 answers

I think you want to highlight the entire text of the selected UITextField. In this case, you must first determine which UITextFieldmethod ( -didBeginEditDescription) has called , and then you can call -selectAllspecific for this UITextField.

Code example:

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

Update:

-selectAllmust work. I have already implemented and works very well for me. Note what you wrote -selectAllinstead -selectAll. You can also try the following:

[txtFld setSelectedTextRange:[txtFld textRangeFromPosition:txtFld.beginningOfDocument toPosition:txtFld.endOfDocument]]; 
+9
source

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 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);            

        }
    }
+3

I found that I had to put a call selectAll:selfon TouchDown:, noteditDidBegin:

Attach Touch Downto - (IBAction)didBeginEditDescription:(id)senderin InterfaceBuilder.

+1
source

For Xamarin.Forms, create your own renderer using 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);
}
0
source

All Articles