[英]Programmatically Select all text in UITextField
如何以编程方式选择 UITextField 中的所有文本?
这就是我的诀窍:
[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];
相当丑陋但它有效,所以不会显示 sharedMenuController !
要解决“每隔一次才有效”的问题,请使用以下命令:
__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];
});
感谢 Eric Baker(刚刚从这里的评论中编辑)
事实证明,使用非零发送者调用 -selectAll: 会显示菜单。 用 nil 调用它会导致它选择文本,但不显示菜单。
在我的错误报告从 Apple 返回并建议我通过 nil 而不是 self 之后,我尝试了这个。
无需使用 UIMenuController 或其他选择 API。
我只是对此进行了测试以验证上面 Mirko 的评论,但是我的测试验证了selectAll:
实际上在将文本发送到 UITextField 本身时确实选择了所有文本。
请注意,文本将立即被 CUT | 遮挡。 复制 | PASTE 操作,但对于您的问题,这正是用户点击“全选”开始时出现的内容。
我要使用的解决方案如下,请注意,第二行将暂时隐藏 CUT/COPY/PASTE 对话框,而不会为明确的用户选择禁用它
[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;
使用你需要的
对象
[yourtextField becomeFirstResponder]; //puts cursor on text field
[yourtextField selectAll:nil]; //highlights text
[yourtextField selectAll:self]; //highlights text and shows menu(cut copy paste)
迅速
yourTextField.becomeFirstResponder() //puts cursor on text field
yourTextField.selectAll(nil) //highlights text
yourTextField.selectAll(self) //highlights text and shows menu(cut copy paste)
这是我找到的最好的解决方案。 没有 sharedMenuController,它连续工作:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}
迅速
选择UITextField
所有文本:
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
我的完整答案在这里: https : //stackoverflow.com/a/34922332/3681880
为了能够选择文本,文本字段必须是可编辑的。 要知道文本字段何时可编辑,请使用委托方法:
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
我不认为 textFieldShouldBeginEditing: 是必需的,但这是我在实现中使用的。
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[textField selectAll:textField];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
将 nil 传递给 selectAll:不会显示菜单。
不幸的是,我认为你不能这样做。
我不确定这是否对您有帮助,但是setClearsOnBeginEditing
允许您指定UITextField
应在用户开始编辑时删除现有值(这是安全UITextFields
的默认值)。
斯威夫特 3:
textField.selectAll(self)
我创建了一个自定义警报视图,其中包含一个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!!!
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 中的文本,那么只需将 firstResponder 分配给它:
[textField becomeFirstResponder]
如果您的意思是如何获取 uitextfield 中的文本,则可以这样做:
textField.text
如果您的意思是实际选择文本(如突出显示),那么这可能很有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.