UISearchBar select all text

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

I want to allow the user to modify the previous search text. At some point, when the user is just starting to enter a new request, the old one should be fired. The standard way to achieve it is to select all the text at the moment you start editing the text.

+6
source share
7 answers

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

If he wants to add text, he naturally lights up again at the end of the existing text.

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

+2
source

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

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

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

 [_mySearchBar becomeFirstResponder] [[UIApplication sharedApplication] sendAction:@selector(selectAll:) to:nil from:nil forEvent:nil] 
+10
source

If you want the "type replace" function to replace text in a UITextField (i.e., the cross is not acceptable), you can dig through the UISearchBar subzones 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]; 
+4
source

In my case, sending selectAll(_:) did not work right after calling becomeFirstResponder .

I worked around it, expecting 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) }) 
+3
source

I don’t think there is a way to select all the text. Perhaps when there is a focus on UISearchBar , you can clear the search bar like this: searchBar.text = @""

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

0
source

You can accomplish this by saving BOOL , indicating that only editing the text field of the search bar has just begun. Then you can catch the first keystroke 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; } 
0
source

Swift 4

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

 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { DispatchQueue.main.async { UIApplication.shared.sendAction(#selector(UITextField.selectAll(_:)), to: nil, from: nil, for: nil) } return true } 
0
source

Source: https://habr.com/ru/post/903403/


All Articles