UISearchBar select all text
Asked Answered
H

7

7

Is there any way to select all text in UISearchBar? I tried [searchBar selectALL:], but it throw the signal (unrecognized selector).

I want to allow user to alter previous search text. At the some time, when user just starts typing new request, the old one should be dismissed. The standard way how to achieve it - select all text at the moment when text begin editing.

Hagiarchy answered 8/12, 2011 at 17:45 Comment(0)
P
2

Here is another suggestion: when someone activates the search bar, there are two possible intentions: type new text or add to the existing text. I think you should give your user the choice.

If he wants to add text he naturally taps again at the end of the existing text.

If he wants to start over, he can press the clear button that automatically appears when the search bar becomes active.

Phenomenalism answered 8/12, 2011 at 19:50 Comment(2)
Well, it is the best solution.Hagiarchy
This behaviour differs from Safari on iOS though, which selects the text when you tap the address bar.Myrwyn
F
12

This can be accomplished using the standard UIResponder semantics. No need to dig down into the private view hierarchy of UISearchBar.

[[UIApplication sharedApplication] sendAction:@selector(selectAll:) to:nil from:nil forEvent:nil]

You can call this from anywhere, and the selectAll: selector will run the responder chain to see if any objects respond to it. Assuming your search bar is currently the first responder (if the user is typing in it), it will respond and the result will be all text selected. If not you can make it the first responder by calling becomeFirstResponder on the search bar.

[_mySearchBar becomeFirstResponder]
[[UIApplication sharedApplication] sendAction:@selector(selectAll:) to:nil from:nil forEvent:nil]
Ferguson answered 5/3, 2015 at 20:26 Comment(0)
C
4

If you want the 'type to replace' functionality that selecting the text in the UITextField gives you (ie the extra tap on the cross is unacceptable), you can dig through the subviews of the UISearchBar to find the UITextField (or UISearchBarTextField) and select its text:

// need to select the searchBar text ... 
UITextField * searchText = nil;
for (UIView *subview in searchBar.subviews) 
{
    // we can't check if it is a UITextField because it is a UISearchBarTextField.
    // Instead we check if the view conforms to UITextInput protocol. This finds
    // the view we are after.
    if ([subview conformsToProtocol:@protocol(UITextInput)]) 
    {
        searchText = (UITextField*)subview;
        break;
    }
}

if (searchText != nil)
    [searchText selectAll:self];
Chau answered 19/1, 2012 at 5:52 Comment(1)
Note that this no longer works in iOS 7 - the text field is now nested deeper in the view hierarchy. To fix this, simply change the code to a recursive version that traverses the hierarchy until it finds the text field. There could also be an issue with the "selectAll:" method, depending on what do you actually want to achieve. See this question for more details. Also note that I had to call this in viewDidAppear, not viewWillAppear for it to work.Webfoot
F
4

In my case, the sending selectAll(_:) didn't work immediately after calling becomeFirstResponder.

I worked around it by waiting one runloop:

Swift 2:

dispatch_async(dispatch_get_main_queue()) {
    UIApplication.sharedApplication().sendAction(#selector(UITextField.selectAll(_:)), to: nil, from: nil, forEvent: nil)
}

Swift 3:

DispatchQueue.main.async(execute: {
    UIApplication.sharedApplication().sendAction(#selector(UITextField.selectAll(_:)), to: nil, from: nil, forEvent: nil)
})
Fonda answered 20/11, 2016 at 12:13 Comment(0)
P
2

Here is another suggestion: when someone activates the search bar, there are two possible intentions: type new text or add to the existing text. I think you should give your user the choice.

If he wants to add text he naturally taps again at the end of the existing text.

If he wants to start over, he can press the clear button that automatically appears when the search bar becomes active.

Phenomenalism answered 8/12, 2011 at 19:50 Comment(2)
Well, it is the best solution.Hagiarchy
This behaviour differs from Safari on iOS though, which selects the text when you tap the address bar.Myrwyn
F
1

Swift 4

If you want to select all text when searchBar become first responder.

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    DispatchQueue.main.async {
        UIApplication.shared.sendAction(#selector(UITextField.selectAll(_:)), to: nil, from: nil, for: nil)
    }
    return true
}
Fossilize answered 7/7, 2019 at 10:21 Comment(0)
I
0

I dont think there is a method to select all text. Maybe when there is a focus on UISearchBar you can clear the search bar like so - searchBar.text = @""

i.e. clear text in the search bar... Hope this helps in some way...

Infuscate answered 8/12, 2011 at 17:56 Comment(1)
In this case there is no way to user to alter request. If it is a long string it can be annoying to retyping it.Hagiarchy
P
0

You can accomplish this by keeping a BOOL indicating if editing the search bar text field just started. Then, you can catch the first key press in the searchBar delegate methods.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
   firstEdit = YES;
}

- (BOOL)searchBar:(UISearchBar *)searchBar 
       shouldChangeTextInRange:(NSRange)range 
       replacementText:(NSString *)text {

   if (firstEdit) {
      searchBar.text = text;
      firstEdit = NO;
   }
   return YES;
}
Phenomenalism answered 8/12, 2011 at 19:23 Comment(1)
Is there any way to highlight text?Hagiarchy

© 2022 - 2024 — McMap. All rights reserved.