以编程方式选择 UITextField 中的所有文本
如何以编程方式选择 UITextField 中的所有文本?
How can I programmatically select all text in UITextField?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何以编程方式选择 UITextField 中的所有文本?
How can I programmatically select all text in UITextField?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
这就是对我来说的窍门:
相当丑陋,但它有效,所以不会显示共享菜单控制器!
要解决“仅每秒有效”问题,请使用以下内容:
感谢埃里克·贝克(刚刚从此处的评论中编辑)
Thats what did the trick for me:
Pretty ugly but it works, so there will be no sharedMenuController shown!
To fix the "only works every second time" problem use following:
Thanks to Eric Baker ( just edited from comment in here )
事实证明,使用非零发送者调用 -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.
使用您需要的
ObjC
Swift
Use what you need
ObjC
Swift
我只是对此进行了测试以验证 Mirko 上面的评论,但我的测试验证了
selectAll:
实际上在发送到 UITextField 本身时选择了所有文本。请注意,文本将立即被 CUT | 遮盖。复制|粘贴操作,但对于您的问题,这正是用户点击“全选”时首先出现的内容。
我要采用的解决方案如下,请注意,第二行将暂时隐藏“剪切/复制/粘贴”对话框,而不会针对显式用户选择禁用它
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
这是我找到的最好的解决方案。没有sharedMenuController,它连续工作:
This is the best solution I've found. No sharedMenuController, and it works consecutively:
Swift
选择
UITextField
中的所有文本:我的完整答案在这里:https: //stackoverflow.com/a/34922332/3681880
Swift
Select all text in a
UITextField
:My full answer is here: https://stackoverflow.com/a/34922332/3681880
为了能够选择文本,文本字段必须是可编辑的。要知道文本字段何时可编辑,请使用委托方法:
我不认为 textFieldShouldBeginEditing: 是必需的,但它是我在实现中使用的。
将 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:
I don't think textFieldShouldBeginEditing: is required but it's what I used in my implementation.
Passing in nil to selectAll: won't show the Menu.
不幸的是我不认为你能做到这一点。
我不确定这是否对您有帮助,但是
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 theUITextField
should delete the existing value when the user starts editing (this is the default for secureUITextFields
).斯威夫特3:
Swift 3:
我创建了一个自定义警报视图,其中包含一个 UITextField 。我发现文本字段的一个问题是:
beginningOfDocument
仅当文本字段添加到屏幕时才有价值。调用becomeFirstResponder
。否则
beginningOfDocument
返回nil
并且[UITextField textRangeFromPosition:]
无法获取该值。这是我解决这种情况的示例代码。
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
returnsnil
and[UITextField textRangeFromPosition:]
can not get the value.So here is my sample code to solve this case.
完毕!
Done!
如果您的意思是如何允许用户编辑 uitextfield 中的文本,那么只需将 firstResponder 分配给它:
如果您的意思是如何获取 uitextfield 中的文本,那么这会做到:
如果您的意思是实际选择文本(如突出显示它)那么这可能会很有用:
selectAll
If you mean how would you allow the user to edit the text in a uitextfield then just assign firstResponder to it:
If you mean how do you get the text in the uitextfield than this will do it:
If you mean actually select the text (as in highlight it) then this will may be useful:
selectAll