键入时如何进行实时UITextField计数(Swift)?

Far*_*had 21 uitextfield uitextview ios swift

当用户使用swift 在UITextField中输入时,我想计算字符数.

字段和标签的图像:

在此输入图像描述

我已经放置了UITextField和UILabel,只是没有找到有关Stack溢出的任何信息,如果你可以在UITextView中做一个我也很感激.

Ica*_*aro 26

要使用下面的功能,您需要UITextFieldDelegate protocol在要计算的文本字段上实现.每次UITextFields文本更改时都会调用它:

你的类声明看起来应该是这样的

class ViewController: UIViewController, UITextFieldDelegate
Run Code Online (Sandbox Code Playgroud)

你应该有@IBOutlet类似的

@IBOutlet var txtValue: UITextField
Run Code Online (Sandbox Code Playgroud)

UITextFields委托设置为self.

override func viewDidLoad() {
    super.viewDidLoad()
    txtValue.delegate = self                
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length

    mylabel.text =  String(newLength) // Set value of the label
    // myCounter = newLength // Optional: Save this value
    // return newLength <= 25 // Optional: Set limits on input. 
    return true
}
Run Code Online (Sandbox Code Playgroud)

请注意,此函数在所有UITextFields 上都被调用,所以如果你有几个UITextFields,你需要添加一个逻辑来知道一个人正在调用这个函数.


Yas*_*ala 16

使用UITextFieldDelegate存在一个非常优雅和整洁的解决方案.我的解决方案使用了选择器的概念.在选择器中,您告诉编译器在特定事件发生时要执行的功能/操作.在这种情况下 - 在文本字段中键入.确保在storyboard中设置了textField委托.

override func viewDidLoad() {
   super.viewDidLoad()
   yourTextField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
}

func textFieldDidChange(textField : UITextField){
   label.text = yourTextField.text?.characters.count
}
Run Code Online (Sandbox Code Playgroud)


Min*_*nae 7

多个 UITextView

在此输入图像描述

class ViewController: UIViewController, UITextViewDelegate {

 @IBOutlet weak var ticketSummaryTV: UITextView!
 @IBOutlet weak var ticketDescriptionTV: UITextView!

 @IBOutlet weak var summaryCountLbl: UILabel!
 @IBOutlet weak var descriptionCountLbl: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    ticketSummaryTV.delegate = self
    ticketDescriptionTV.delegate = self

    self.updateCharacterCount()
 }

 func updateCharacterCount() {
    let summaryCount = self.ticketSummaryTV.text.characters.count
    let descriptionCount = self.ticketDescriptionTV.text.characters.count

    self.summaryCountLbl.text = "\((0) + summaryCount)/50"

    self.descriptionCountLbl.text = "\((0) + descriptionCount)/500"
 }

 func textViewDidChange(_ textView: UITextView) {
    self.updateCharacterCount()
 }


 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
    if(textView == ticketSummaryTV){
       return textView.text.characters.count +  (text.characters.count - range.length) <= 50
    }else if(textView == ticketDescriptionTV){
        return textView.text.characters.count +  (text.characters.count - range.length) <= 500
    }
    return false
 }
}
Run Code Online (Sandbox Code Playgroud)


Leo*_*Leo 5

这只会让你的文本字段输入14个字符

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    self.label.text = "14"
    self.textfield.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
    if(newLength <= 14){
        self.label.text = "\(14 - newLength)"
        return true
    }else{
        return false
    }
}
}
Run Code Online (Sandbox Code Playgroud)

和截图

在此输入图像描述