有没有办法以编程方式启用或禁用UIKeyboard
上的Return键?我能找到的最接近的是enablesReturnKeyAutomatically
,但这只能说明是否禁用它。
发布于 2012-07-08 19:57:42
也许下面的代码段会有所帮助:
textfield.enablesReturnKeyAutomatically = YES;
这是在UITextInputTraits的iPhone SDK中公开提供的。使用此选项,当文本字段中没有可用的输入文本时,将禁用return键。
发布于 2017-09-01 23:52:13
您可以覆盖UITextField
的hasText
属性来实现这一点:
class CustomTextField : UITextField {
override public var hasText: Bool {
get {
return evaluateString(text)
}
}
}
其中evaluateString(_ text: String?) -> Bool
会根据您需要的输入条件进行检查,例如字符数。
当然,这只能与UITextField
上设置的enablesReturnKeyAutomatically = true
结合使用。
我知道我的答案既不及时,也不是用Objective-C写的,但考虑到我在其他地方找不到答案,这个问题经常在其他帖子中被引用,我认为这里是发布它的最好地方。
发布于 2018-05-11 03:59:50
以下是文档API中提供的一种技术,但是当enter键被禁用时,它不能提供可视反馈。
- (void)setup {
// Or in init
self.textField.delegate = self;
}
// <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// substitute your test here
return [textField.text rangeOfString:@"@"].location != NSNotFound;
}
这里的其他答案可以与
[textField addTarget:self
action:@selector(validateTextField:)
forControlEvents:UIControlEventEditingChanged];
以便在用户键入时提供动态视觉反馈。
https://stackoverflow.com/questions/788323
复制相似问题