[ad_1]
How can I modify the colour solely of additional letters when typing in UITextField in Swift and return the colour again when deleting?
I used to be in a position solely to alter coloration of all letters, when exceeding the restrict
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var inputLimitField: UITextField!
@IBOutlet weak var characterCountLabel: UILabel!
override func viewDidLoad() {
tremendous.viewDidLoad()
characterCountLabel.textual content = "10/10"
inputLimitField.delegate = self
}
func checkRemainingChars() {
let allowedChars = 10
let charsInTextView = -(inputLimitField.textual content?.depend ?? 0)
let remainingChars = allowedChars + charsInTextView
characterCountLabel.textColor = .black
inputLimitField.textColor = .black
inputLimitField.layer.borderColor = UIColor.systemGray3.cgColor
if remainingChars < 0 {
characterCountLabel.textColor = .pink
inputLimitField.textColor = .pink
inputLimitField.layer.borderColor = UIColor.pink.cgColor
inputLimitField.layer.cornerRadius = 6.0
inputLimitField.layer.borderWidth = 1.0
}
characterCountLabel.textual content = ("(String(remainingChars))/10")
}
func textFieldDidChangeSelection(_ textField: UITextField) {
checkRemainingChars()
}
}
that is what I anticipated to get
enter picture description right here
and that is what I actually get enter picture description right here
p.s. additionally I am unable to put a letter counter within the nook by TF’s border, as a result of there’s a mistake after I import Materials Parts: No such module 'MaterialComponents'
[ad_2]
