Sausage Machine Sausage Machine - 1 year ago 77
iOS Question

Setting prompt to beginning of text in text field

I'm trying to set the the prompt to the beginning of some text in a UITextField based on this accepted answer Getting and Setting Cursor Position of UITextField and UITextView in Swift

However the prompt is getting set at the end, not the beginning. Has something changed in iOS10/Swift 3 to invalidate the code in the answer in that question?

@IBOutlet weak var mdnTextField: UITextField!

mdnTextField.text = "Some text"
mdnTextField.tintColor = UIColor.textFieldPromptLightGray()
mdnTextField.textColor = UIColor.textFieldPromptLightGray()
let promptPosition = mdnTextField.beginningOfDocument
mdnTextField.selectedTextRange = mdnTextField.textRange(from: promptPosition, to: promptPosition)

Answer Source

Move the promptPosition stuff to your text field's delegate's didBeginEditing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download