ios 以编程方式选择 UITextField 中的所有文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1689911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:35:37  来源:igfitidea点击:

Programmatically Select all text in UITextField

iosswiftuitextfielduikit

提问by Mirko

How can I programmatically select all text in UITextField?

如何以编程方式选择 UITextField 中的所有文本?

采纳答案by Rick

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.

事实证明,使用非零发送者调用 -selectAll: 会显示菜单。用 nil 调用它会导致它选择文本,但不显示菜单。

I tried this after my bug report about it came back from Apple with the suggestion that I pass nil instead of self.

在我的错误报告从 Apple 返回并建议我通过 nil 而不是 self 之后,我尝试了这个。

No need to muck with UIMenuController or other selection APIs.

无需使用 UIMenuController 或其他选择 API。

回答by blackforestcowboy

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!

相当丑陋但它有效,所以不会显示 sharedMenuController !

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 )

感谢 Eric Baker(刚刚从这里的评论中编辑)

回答by Justin Searls

I just tested this to verify Mirko's comment above, but my test verifies that selectAll:does in fact select all the text when it's sent to the UITextField itself.

我刚刚测试了这个以验证上面 Mirko 的评论,但是我的测试验证了它selectAll:确实在将文本发送到 UITextField 本身时选择了所有文本。

Note that the text will be immediately obscured with CUT | COPY | PASTE actions, but to your question, it is exactly what appears when a user taps "Select All" to begin with.

请注意,文本将立即被 CUT | 遮挡​​。复制 | PASTE 操作,但对于您的问题,这正是用户点击“全选”开始时出现的内容。

The solution I'm going with follows, note that the second line will temporarily hide the CUT/COPY/PASTE dialog, without disabling it for explicit user selections

我要使用的解决方案如下,请注意,第二行将暂时隐藏 CUT/COPY/PASTE 对话框,而不会为明确的用户选择禁用它

[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;

回答by Ted

Use what you need

使用你需要的

ObjC

对象

 [yourtextField becomeFirstResponder]; //puts cursor on text field

 [yourtextField selectAll:nil];  //highlights text

 [yourtextField selectAll:self]; //highlights text and shows menu(cut copy paste)

Swift

迅速

yourTextField.becomeFirstResponder() //puts cursor on text field

yourTextField.selectAll(nil)  //highlights text

yourTextField.selectAll(self) //highlights text and shows menu(cut copy paste)

回答by Suragch

Swift

迅速

Select all text in a UITextField:

选择 a 中的所有文本UITextField

textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)

My full answer is here: https://stackoverflow.com/a/34922332/3681880

我的完整答案在这里:https: //stackoverflow.com/a/34922332/3681880

回答by Jase68

This is the best solution I've found. No sharedMenuController, and it works consecutively:

这是我找到的最好的解决方案。没有 sharedMenuController,它连续工作:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}

回答by Jason Daniels

To be able to select text, the text field has to be editable. To know when the text field is editable use the delegate methods:

为了能够选择文本,文本字段必须是可编辑的。要知道文本字段何时可编辑,请使用委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

I don't think textFieldShouldBeginEditing: is required but it's what I used in my implementation.

我不认为 textFieldShouldBeginEditing: 是必需的,但这是我在实现中使用的。

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField selectAll:textField];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

Passing in nil to selectAll: won't show the Menu.

将 nil 传递给 selectAll:不会显示菜单。

回答by John Scalo

Swift 3:

斯威夫特 3:

textField.selectAll(self)

回答by Dan J

Unfortunately I don't think you can do that.

不幸的是,我认为你不能这样做。

I'm not sure if this helps you, but setClearsOnBeginEditinglets you specify that the UITextFieldshould delete the existing value when the user starts editing (this is the default for secure UITextFields).

我不确定这是否对您有帮助,但setClearsOnBeginEditing让您指定UITextField在用户开始编辑时应删除现有值(这是 secure 的默认值UITextFields)。

回答by nahung89

I create a custom alert view which contains a UITextFieldinside. I found a problem to the textfield is that: beginningOfDocumentonly has value if textfield is added to screen & becomeFirstResponderis called.

我创建了一个包含UITextField内部的自定义警报视图。我发现文本字段的一个问题是:beginningOfDocument只有在将文本字段添加到屏幕 &becomeFirstResponder被调用时才有价值。

Otherwise beginningOfDocumentreturns niland [UITextField textRangeFromPosition:]can not get the value.

否则,beginningOfDocument返回nil[UITextField textRangeFromPosition:]不能得到的价值。

So here is my sample code to solve this case.

所以这是我解决这种情况的示例代码。

UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
[window addSubview:theAlertView]; // textfield must be added as a subview of screen first
UITextField *textField = theAlertView.textField;
[textField becomeFirstResponder]; // then call to show keyboard and cursor 
UITextRange *range = [textField textRangeFromPosition:textField.beginningOfDocument toPosition:textField.endOfDocument]; // at this time, we could get beginningOfDocument
[textField setSelectedTextRange:range]; // Finally, it works!!!