如果 UITextField 被隐藏,则重置 UILabel 左侧的字符

Reset characters left UILabel if the UITextField is being hidden

我有 5 个UITextFields。单击按钮时,2 个可见,3 个可能是额外的 "added"(基本上是 .isHidden = false,但给人一种额外添加的错觉)。 3 个额外的 UITextFields 也有一个删除按钮(作为 rightView),当它被点击时,在动画中制作 .isHidden = true

现在每个 UITextField 都有一个 UILabel(显示剩余字符的 rightView)。问题是当用户点击那个删除按钮和相应的 UITextField再次隐藏,但其余字符 UILabel 保留其旧值。我不确定如何重置它。这是代码:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else { return true }
    let newLength = text.count + string.count - range.length

    var rightView = UIView()
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    label.font = UIFont(name: Fonts.OpenSans_Light, size: 14)

    if textField === firstChoiceTextField || textField === secondChoiceTextField {
        rightView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 25))
    }

    if textField === thirdChoiceTextField || textField === forthChoiceTextField || textField === fifthChoiceTextField {
        rightView = UIView(frame: CGRect(x: 0, y: 0, width: 55, height: 25))
        let button = UIButton(frame: CGRect(x: rightView.frame.width - 30, y: 0, width: 25, height: 25))
        button.setBackgroundImage(UIImage(named: "icon_cancel_dark"), for: .normal)
        button.addTarget(self, action: #selector(self.hideTextField(_:)), for: .touchUpInside)
        rightView.addSubview(button)
    }

    rightView.addSubview(label)
    textField.rightView = rightView
    textField.rightViewMode = .always
    label.textAlignment = .center

    // when the text field has been hidden with the hideTextField method below, the characters left label still shows the old count if the label is being shown again

    if newLength <= 35 {
        label.text =  String(50 - newLength)
        label.textColor = .lightGray
    }
    else {
        label.text =  String(50 - newLength)
        label.textColor = UIColor.red
    }

    return newLength < 50
}


@objc func hideTextField(_ sender: UIButton) {
    if let  field = sender.superview?.superview as? UITextField, !field.isHidden {
        UIView.animate(withDuration: 0.2) {
            field.text = ""
            field.isHidden = true
        }
    }
    if !self.thirdChoiceTextField.isHidden && !self.forthChoiceTextField.isHidden && !self.fifthChoiceTextField.isHidden {
        self.addTextFieldButton.isEnabled = false
    }
    else{
        self.addTextFieldButton.isEnabled = true
    }
}

hideTextField(_ sender: UIButton)方法中,你可以从UITextfieldrightView中获取label,如下所示

    if let  field = sender.superview?.superview as? UITextField, !field.isHidden {

        if let label = field.rightView?.subviews.filter({ [=10=] is UILabel }).first as? UILabel {
            print("I am the label. reset me")
            label.text = ""
        }
        UIView.animate(withDuration: 0.2) {
            field.text = ""
            field.isHidden = true
        }
    }