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 🙂
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
Your question duplicates this. The best answer there is to use delegate functions textFieldShouldBeginEditing and textFieldShouldEndEditing to change and restore colors.
set delegate for textfield
add code in delegate
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.
}
In your ViewController you need to link-up your textfield’s delegate to itself.
}
Output: