skip to Main Content

I want the text in a textfield to be highlighted when the textfield is pressed, just like when you press the website text in a browser. I tried to google but couldn’t find a direct answer for swift/xcode.

Thankful for any help 🙂

3

Answers


  1. Your question duplicates this. The best answer there is to use delegate functions textFieldShouldBeginEditing and textFieldShouldEndEditing to change and restore colors.

    Login or Signup to reply.
  2. set delegate for textfield

    TF.delegate = self
    

    add code in delegate

    extension viewcontroller: UITextFieldDelegate {
        func textFieldDidBeginEditing(_ textField: UITextField) {
            textField.layer.borderWidth = 2
            textField.layer.borderColor = global().defaultColor().cgColor
        }
        
        func textFieldDidEndEditing(_ textField: UITextField) {
            textField.layer.borderWidth = 0
        }
    }
    
    Login or Signup to reply.
  3. textFieldDidBeginEditing

    This is not going to achieve what you exactly want because according to UITextField life-cycle, this method will be triggered exactly when editing started and about to be end. What you need is, highlight exact moment with tapping textfield.

    First separate your highlighted and normal state appearences.

    extension MainView {
    func textFieldActiveDisplay() {
    
        self.isTextFieldOpen = true
        let color = COLOR
        let animation: CABasicAnimation = CABasicAnimation(keyPath: "borderColor")
        animation.fromValue = textField.layer.borderColor
        animation.toValue = color
        animation.duration = 0.3
        textField.layer.borderColor = color.cgColor
        
        let borderWidth: CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
        borderWidth.fromValue = 0
        borderWidth.toValue = 4
        borderWidth.duration = 0.2
        searchBar.layer.borderWidth = 4
        
        let group = CAAnimationGroup()
        group.animations = [animation, borderWidth]
        searchBar.layer.add(group, forKey: nil)
        
        textField.font = HIGHLIGHTED_FONT 
        textField.textColor = HIGHLIGHTED_COLOR
    }
    
    func textFieldInActiveDisplay() {
        guard isTextFieldOpen else { return }
        let borderWidth: CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
        borderWidth.fromValue = 4
        borderWidth.toValue = 0
        borderWidth.duration = 0.2
        textField.layer.borderWidth = 0
        textField.layer.add(borderWidth, forKey: nil)
        
        textField.font = NORMAL_FONT
        textField.textColor = NORMAL_COLOR
        self.isTextFieldOpen = false
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            self.textField.subviews.forEach({$0.layer.removeAllAnimations()})
            self.textField.layer.removeAllAnimations()
            self.textField.layoutIfNeeded()
        }
    }
    

    }

    In your ViewController you need to link-up your textfield’s delegate to itself.

    override func viewDidLoad() {
      super.viewDidLoad() 
      view.textField.delegate = self
    }
    
    
    extension MainViewController: UITextFieldDelegate, UIGestureRecognizerDelegate {
        
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        view.textFieldActiveDisplay()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        view.textFieldInActiveDisplay()
    }
    

    }

    Output:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search