当我加载一个UIView时,我如何在iPhone SDK上设置UITextField的最大字符数?


当前回答

这限制了字符的数量,但也确保您可以粘贴字段,直到最大限制。

- (void)textViewDidChange:(UITextView *)textView
{
    NSString* str = [textView text];
    str = [str substringToIndex:MIN(1000,[str length])];
    [textView setText:str];

    if([str length]==1000) {
        // show some label that you've reached the limit of 1000 characters
    }
}

其他回答

虽然UITextField类没有max length属性,但是通过设置文本字段的委托并实现以下委托方法来获得这个功能相对简单:

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;
}

斯威夫特

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    let currentCharacterCount = textField.text?.count ?? 0
    if range.length + range.location > currentCharacterCount {
        return false
    }
    let newLength = currentCharacterCount + string.count - range.length
    return newLength <= 25
}

在文本字段更改之前,UITextField会询问委托是否应该更改指定的文本。此时文本字段还没有改变,所以我们获取它的当前长度和我们插入的字符串长度(通过粘贴复制的文本或使用键盘输入单个字符),减去范围长度。如果该值太长(本例中超过25个字符),则返回NO以禁止更改。

当在文本字段的末尾输入单个字符时,范围。Location是当前字段的长度和范围。Length将为0,因为我们没有替换或删除任何东西。插入到文本字段的中间只是意味着一个不同的范围。位置,粘贴多个字符意味着字符串中有多个字符。

删除单个字符或删除多个字符由一个长度非零的范围和一个空字符串指定。替换只是一个非空字符串的范围删除。

关于崩溃的“撤消”错误的说明

正如评论中提到的,UITextField有一个bug,可能导致崩溃。

如果粘贴到字段,但验证实现阻止了粘贴,则粘贴操作仍会记录在应用程序的撤销缓冲区中。如果你随后触发一个撤销(通过摇动设备并确认撤销),UITextField将尝试用一个空字符串替换它认为它粘贴到自己的字符串。这将会崩溃,因为它从未将字符串粘贴到自身。它将尝试替换字符串中不存在的部分。

幸运的是,你可以保护UITextField不像这样杀死自己。您只需要确保它建议替换的范围确实存在于其当前字符串中。这就是上面最初的健全性检查所做的工作。

Swift 3.0与复制和粘贴工作良好。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let str = (textView.text + text)
        if str.characters.count <= 10 {
            return true
        }
        textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
        return false
    }

希望对你有帮助。

为了完成八月的回答,建议函数的一个可能实现(参见UITextField的委托)。

我没有测试domness代码,但我的代码不会卡住,如果用户达到了限制,它与一个新的字符串兼容,取代一个更小或等于一个。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //limit the size :
    int limit = 20;
    return !([textField.text length]>limit && [string length] > range.length);
}

适用于Swift 3.1或更高版本

首先添加协议UITextFieldDelegate

如:-

class PinCodeViewController: UIViewController, UITextFieldDelegate { 
.....
.....
.....

}

之后创建你的UITextField并设置委托

完成经验:-

import UIKit

class PinCodeViewController: UIViewController, UITextFieldDelegate {

let pinCodetextField: UITextField = {
    let tf = UITextField()
    tf.placeholder = "please enter your pincode"
    tf.font = UIFont.systemFont(ofSize: 15)
    tf.borderStyle = UITextBorderStyle.roundedRect
    tf.autocorrectionType = UITextAutocorrectionType.no
    tf.keyboardType = UIKeyboardType.numberPad
    tf.clearButtonMode = UITextFieldViewMode.whileEditing;
    tf.contentVerticalAlignment = UIControlContentVerticalAlignment.center
 return tf
  }()


 override func viewDidLoad() {
   super.viewDidLoad()
   view.addSubview(pinCodetextField)
    //----- setup your textfield anchor or position where you want to show it----- 


    // after that 
pinCodetextField.delegate = self // setting the delegate

 }
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return !(textField.text?.characters.count == 6 && string != "")
     } // this is return the maximum characters in textfield 
    }

斯威夫特3.0

当您粘贴字符串超过您的字符限制时,此代码工作正常。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let str = (textView.text + text)
        if str.characters.count <= 10 {
            return true
        }
        textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
        return false
    }

谢谢你的投票。:)

在Swift 5.2中工作:

 class AngListVC: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var angTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        angTextField.delegate = self
        angTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
      
    }

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
           let userText =  angTextField.text ?? ""
           var newText = ""
           if range.length > 0 {
               let txt = NSString(string: userText)
               if txt.length > 0 {
                   newText = txt.replacingCharacters(in: range, with: "")
               }
           } else {
               newText = userText + ""
           }
           return newText.count <= 3
       }

@objc func textFieldDidChange(_ textField: UITextField) {
        print("textFieldDidChange")
   }