I'm changing the content of a UITextView with the following code:
mainTextField.replaceRange((theRange), withText: newStr)
Then I would like to select the new text. I'm trying to use the following code:
mainTextField.becomeFirstResponder()
let startPosition = mainTextField.positionFromPosition(mainTextField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: startingPoint)
let endPosition = mainTextField.positionFromPosition(mainTextField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: endingPoint)
if startPosition != nil && endPosition != nil {
mainTextField.selectedTextRange = mainTextField.textRangeFromPosition(startPosition!, toPosition: endPosition!)
}
The problem is that I don't know how to get the startingPoint and endingPoint values. Is it possible to get them in some ways from the replaceRange(range: UITextRange, withText: String) statement?
EDIT: To try to clarify a bit my question:
// mainTextField content = "Have a good day"
// mainTextField selected text = "good"
mainTextField.replaceRange((theRange), withText: "happy")
// here theRange refers the selected text "good" not the new one "happy"
How to select the new text ("happy")?