ios – change coloration of additional characters in UITextField


How can I alter the colour solely of additional letters when typing in UITextField in Swift and return the colour again when deleting?
I used to be ready 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() {
        characterCountLabel.textual content = "10/10"
        inputLimitField.delegate = self

    func checkRemainingChars() {
        let allowedChars = 10
        let charsInTextView = -(inputLimitField.textual content?.rely ?? 0)
        let remainingChars = allowedChars + charsInTextView
        characterCountLabel.textColor = .black
        inputLimitField.textColor = .black
        inputLimitField.layer.borderColor = UIColor.systemGray3.cgColor
        if remainingChars < 0 {
            characterCountLabel.textColor = .crimson
            inputLimitField.textColor = .crimson
            inputLimitField.layer.borderColor = UIColor.crimson.cgColor
            inputLimitField.layer.cornerRadius = 6.0
            inputLimitField.layer.borderWidth = 1.0
        characterCountLabel.textual content = ("(String(remainingChars))/10")
    func textFieldDidChangeSelection(_ textField: UITextField) {

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 can not put a letter counter within the nook via TF’s border, as a result of there’s a mistake once I import Materials Parts: No such module 'MaterialComponents'


Please enter your comment!
Please enter your name here