UITextField Swift Show error FloatLabelTextField

0

I'm using the class I take out of GitHub

FloatLabelTextField
  

link

Which has the effect of raising the placeholder as a title

What I need is to be able to show a detail of error when validating, I would like to put it down, currently the fix I found is to use the following methods  which are an extension of UIVIEW

  func addBottomBorderError(errorDetail: String) {

        let height = 1.5
        let color = UIColor.red
        let separation = 0
        let border = UIView()
        border.backgroundColor = color
        border.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(border)

        let label : UILabel = UILabel()
        label.text = errorDetail
        label.font = UIFont.systemFont(ofSize: 12)
        label.numberOfLines = 2
        label.frame = CGRect(x:0, y: 0, width: 250, height: 20)
        label.textColor = UIColor.red

        border.addSubview(label)

        border.addConstraint(NSLayoutConstraint(item: border,
                                                attribute: NSLayoutAttribute.height,
                                                relatedBy: NSLayoutRelation.equal,
                                                toItem: nil,
                                                attribute: NSLayoutAttribute.height,
                                                multiplier: 1, constant: CGFloat(height)))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.bottom,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.bottom,
                                              multiplier: 1, constant: CGFloat(separation)))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.leading,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.leading,
                                              multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.trailing,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.trailing,
                                              multiplier: 1, constant: 0))
    }
    func addBottomBorderCleanError(textField: FloatLabelTextField) {

        let height = 1.5
        let color = Utils.Color.COLOR_82C341
        let separation = 0
        let border = UIView()
        border.backgroundColor = color
        border.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(border)


        textField.detail = "ff"
        textField.detailColor = Utils.Color.COLOR_2B5034

//        label.text = textField.detail
//        label.font = UIFont.systemFont(ofSize: 12)
//        label.numberOfLines = 2
//        label.frame = CGRect(x:0, y: 0, width: 250, height: 20)
//        label.textColor = UIColor.red
//
        border.addSubview(textField.detailLabel)


        border.addConstraint(NSLayoutConstraint(item: border,
                                                attribute: NSLayoutAttribute.height,
                                                relatedBy: NSLayoutRelation.equal,
                                                toItem: nil,
                                                attribute: NSLayoutAttribute.height,
                                                multiplier: 1, constant: CGFloat(height)))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.bottom,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.bottom,
                                              multiplier: 1, constant: CGFloat(separation)))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.leading,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.leading,
                                              multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: border,
                                              attribute: NSLayoutAttribute.trailing,
                                              relatedBy: NSLayoutRelation.equal,
                                              toItem: self,
                                              attribute: NSLayoutAttribute.trailing,
                                              multiplier: 1, constant: 0))
    }

The problem with this is that as it is added dynamically I can not access the label that it generates to erase the data, and to put a view at the top to make it seem that it eliminates the text does not seem good to me,

My question is you have a better idea of how to add this text below the boxes and then delete them, the method that sets the error is done by pressing the button, and the second is called in the change text event from the box, any idea suits me, I can not use other things because my box already uses the Float for the effect, thank you already

    
asked by Bruno Sosa Fast Tag 11.05.2018 в 19:59
source

1 answer

1

You should make label a property of your class (I would change the name to errorLabel or something more descriptive), to be able to access it outside the method.

class FloatLabelTextField {
    var errorLabel: UILabel?

    func addBottomBorderError(errorDetail: String) {
        ...

        let label = UILabel()
        label.text = errorDetail
        label.font = UIFont.systemFont(ofSize: 12)
        label.numberOfLines = 2
        label.frame = CGRect(x:0, y: 0, width: 250, height: 20)
        label.textColor = UIColor.red

        border.addSubview(label)

        errorLabel = label

        ...
    }
}
    
answered by 11.05.2018 / 20:15
source