The color of the text in UITextfield does not change, not in focus

So, I have two UITextFields: name and amount and two UIButtons: income, expense.
When I click the expense button, I want my text color to amountchange to red or green if the income button is pressed.

This only works if the text field amountis in focus, if the text field nameis in focus, the color does not change for the quantity.

Is there a way to change the color of a text box if it is out of focus?

edit:

Here is my code where I change color:

@IBAction func typeBtnPressed(_ sender: UIButton) {
    if sender.tag == Buttons.expense.rawValue {
        amountTxt.textColor = .red
    } else {
        amountTxt.textColor = .green
    }
}
+4
source share
2 answers

, iOS Text, , , , textColor,

let color: UIColor

if sender.tag == Buttons.expense.rawValue {
    color = .red
} else {
    color = .green
}

let attributedText = NSMutableAttributedString(attributedString: amountTxt.attributedText!)

attributedText.setAttributes([NSAttributedStringKey.foregroundColor : color], range: NSMakeRange(0, attributedText.length))

amountTxt.attributedText = attributedText

,

+3

textColor .

textField.color = newColor
textField.text = text
+4

Source: https://habr.com/ru/post/1687478/


All Articles