Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all text in a UITextField using Swift

Tags:

How do you programatically select all the text in a Swift text field?

var textField:UITextField = UITextField() textField.frame = CGRectMake(2, 25, 200, 20) textField.text = "hello" textField.font = UIFont(name: "Verdana", size: 12) textField.textColor = UIColor.blackColor() textField.backgroundColor = UIColor.whiteColor() 

Or at least make the text immediately editable?

Thanks

Andrew

like image 768
iphaaw Avatar asked Aug 19 '14 13:08

iphaaw


2 Answers

It works in the same way like in Objective-C:

textField.becomeFirstResponder()  textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument) 
like image 100
B.S. Avatar answered Oct 17 '22 23:10

B.S.


textField.becomeFirstResponder() textField.selectAll(nil) 
like image 31
Al___ Avatar answered Oct 17 '22 22:10

Al___