设置UITextField的最大字符长度 [英] Set the maximum character length of a UITextField
问题描述
当我加载一个 UIView
UITextField >
How can I set the maximum amount of characters in a UITextField
on the iPhone SDK when I load up a UIView
?
推荐答案
虽然UITextField类没有max length属性,但通过设置文本字段的 delegate
并实现以下委托方法:
While the UITextField class has no max length property, it's relatively simple to get this functionality by setting the text field's delegate
and implementing the following delegate method:
Objective-C
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
Swift
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 25
}
在文本字段更改之前,UITextField询问代表是否更改指定的文本 。文本字段在这一点上没有改变,所以我们抓住它的当前长度和我们插入的字符串长度减去范围长度。如果此值太长(在此示例中超过25个字符),请返回 NO
以禁止更改。
Before the text field changes, the UITextField asks the delegate if the specified text should be changed. The text field has not changed at this point, so we grab it's current length and the string length we're inserting, minus the range length. If this value is too long (more than 25 characters in this example), return NO
to prohibit the change.
当在文本字段的末尾输入单个字符时, range.location
将是当前字段的长度, range.length
将为0,因为我们不会替换/删除任何东西。插入到文本字段的中间只是意味着不同的 range.location
,并且粘贴多个字符只是意味着 string
When typing in a single character at the end of a text field, the range.location
will be the current field's length, and range.length
will be 0 because we're not replacing/deleting anything. Inserting into the middle of a text field just means a different range.location
, and pasting multiple characters just means string
has more than one character in it.
删除单个字符或剪切多个字符由范围
指定, -zero length和一个空字符串。
Deleting single characters or cutting multiple characters is specified by a range
with a non-zero length, and an empty string. Replacement is just a range deletion with a non-empty string.
正如注释中提到的,有一个错误, UITextField
可能导致崩溃。
As is mentioned in the comments, there is a bug with UITextField
that can lead to a crash.
如果您粘贴到字段,但您的验证实现阻止了粘贴,则粘贴操作仍会记录在应用程序的撤消缓冲区中。如果您随后触发撤消(通过摇动设备并确认撤消), UITextField
将尝试替换其粘贴的字符串到一个空字符串。这将崩溃,因为它从来没有实际上将字符串粘贴到自己。它会尝试替换不存在的字符串的一部分。
If you paste in to the field, but the paste is prevented by your validation implementation, the paste operation is still recorded in the application's undo buffer. If you then fire an undo (by shaking the device and confirming an Undo), the UITextField
will attempt to replace the string it thinks it pasted in to itself with an empty string. This will crash because it never actually pasted the string in to itself. It will try to replace a part of the string that doesn't exist.
幸运的是,你可以保护UITextField不会像这样杀死。您只需要确保其建议替换的范围存在于当前字符串中。这是上面的初始健康检查。
Fortunately you can protect the UITextField from killing itself like this. You just need to ensure that the range it proposes to replace does exist within its current string. This is what the initial sanity check above does.
这篇关于设置UITextField的最大字符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!