要修复UITextField中的最大字符数,您可以实现UITextField Delegate方法textField:shouldChangeCharactersInRange
,如果用户尝试编辑超过固定长度的字符串,则返回false.
//Assume myTextField is a UITextField
myTextField.delegate = self;
//implement this UITextFiledDelegate Protocol method in the same class
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > kMaxTextFieldStringLength)
return NO;
else
return YES;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3612 次 |
最近记录: |