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

发布于 2024-08-10 05:47:21 字数 36 浏览 9 评论 0原文

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

How can I programmatically select all text in UITextField?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

无边思念无边月 2024-08-17 05:47:21

这就是对我来说的窍门:

[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];

相当丑陋,但它有效,所以不会显示共享菜单控制器!

要解决“仅每秒有效”问题,请使用以下内容:

__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];
});

感谢埃里克·贝克(刚刚从此处的评论中编辑)

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 )

俏︾媚 2024-08-17 05:47:21

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

在我从苹果返回有关它的错误报告并建议我传递 nil 而不是 self 后,我尝试了这个。

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

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.

悲念泪 2024-08-17 05:47:21

使用您需要的

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)

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)
摇划花蜜的午后 2024-08-17 05:47:21

我只是对此进行了测试以验证 Mirko 上面的评论,但我的测试验证了 selectAll: 实际上在发送到 UITextField 本身时选择了所有文本。

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

我要采用的解决方案如下,请注意,第二行将暂时隐藏“剪切/复制/粘贴”对话框,而不会针对显式用户选择禁用它

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

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.

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.

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

[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;
梦醒灬来后我 2024-08-17 05:47:21

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

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

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

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}
长途伴 2024-08-17 05:47:21

Swift

选择 UITextField 中的所有文本:

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

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

Swift

Select all text in a UITextField:

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

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

行雁书 2024-08-17 05:47:21

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

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

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

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

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

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

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.

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

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

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

旧夏天 2024-08-17 05:47:21

不幸的是我不认为你能做到这一点。

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

Unfortunately I don't think you can do that.

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

辞旧 2024-08-17 05:47:21

斯威夫特3:

textField.selectAll(self)

Swift 3:

textField.selectAll(self)
隱形的亼 2024-08-17 05:47:21

我创建了一个自定义警报视图,其中包含一个 UITextField 。我发现文本字段的一个问题是: beginningOfDocument 仅当文本字段添加到屏幕时才有价值。调用 becomeFirstResponder

否则 beginningOfDocument 返回 nil 并且 [UITextField textRangeFromPosition:] 无法获取该值。

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

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!!!

I create a custom alert view which contains a UITextField inside. I found a problem to the textfield is that: beginningOfDocument only has value if textfield is added to screen & becomeFirstResponder is called.

Otherwise beginningOfDocument returns nil and [UITextField textRangeFromPosition:] can not get the value.

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!!!
酒解孤独 2024-08-17 05:47:21
UITextField *tf = yourTF;
// hide cursor (you have store default color!!!)
[[tf valueForKey:@"textInputTraits"] setValue:[UIColor clearColor]
                                       forKey:@"insertionPointColor"];
// enable selection
[tf selectAll:self];
// insert your string here
// and select nothing (!!!)
[tf setMarkedText:@"Egor"
    selectedRange:NSMakeRange(0, 0)];

完毕!

UITextField *tf = yourTF;
// hide cursor (you have store default color!!!)
[[tf valueForKey:@"textInputTraits"] setValue:[UIColor clearColor]
                                       forKey:@"insertionPointColor"];
// enable selection
[tf selectAll:self];
// insert your string here
// and select nothing (!!!)
[tf setMarkedText:@"Egor"
    selectedRange:NSMakeRange(0, 0)];

Done!

独孤求败 2024-08-17 05:47:21

如果您的意思是如何允许用户编辑 uitextfield 中的文本,那么只需将 firstResponder 分配给它:

[textField becomeFirstResponder]

如果您的意思是如何获取 uitextfield 中的文本,那么这会做到:

textField.text

如果您的意思是实际选择文本(如突出显示它)那么这可能会很有用:

selectAll

If you mean how would you allow the user to edit the text in a uitextfield then just assign firstResponder to it:

[textField becomeFirstResponder]

If you mean how do you get the text in the uitextfield than this will do it:

textField.text

If you mean actually select the text (as in highlight it) then this will may be useful:

selectAll

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文