设置UITextField的最大字符长度

编程入门 行业动态 更新时间:2024-10-28 16:23:04
本文介绍了设置UITextField的最大字符长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我加载一个 UIView

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的最大字符长度

本文发布于:2023-06-05 11:34:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/518802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   字符   UITextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!