How can I programmatically select all text in UITextField?
Thats what did the trick for me:
[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];
Pretty ugly but it works, so there will be no sharedMenuController shown!
To fix the "only works every second time" problem use following:
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument];
[strongSelf setSelectedTextRange:range];
});
Thanks to Eric Baker ( just edited from comment in here )
Turns out, calling -selectAll: with a non-nil sender displays the menu. Calling it with nil causes it to select the text, but not display the menu.
I tried this after my bug report about it came back from Apple with the suggestion that I pass nil instead of self.
No need to muck with UIMenuController or other selection APIs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With