Saturday, June 29, 2024
HomeiOS Developmentios - Setting the cursor to the tip of a UITextField on...

ios – Setting the cursor to the tip of a UITextField on develop into first responder

[ad_1]

I’ve a UITextField with a textual content alignment set to proper embedded in a UICollectionViewCell. If I faucet the UITextField the cursor must be on the finish of the textual content. Additionally if I faucet the textfield on or earlier than of the start of the textual content.
I attempted to set the place within the textFieldDidBeginEditing delegate technique:

func textFieldDidBeginEditing(_ textField: UITextField) {
    let place = textField.endOfDocument
    textField.selectedTextRange = textField.textRange(from: place, to: place)
}

However this did not work so I attempted it with DispatchQueue.foremost.async. That works. However I do know, that this is not essentially the most elegant approach and it seems to be like one thing tries to set the cursor place on the similar time.

func textFieldDidBeginEditing(_ textField: UITextField) {
    DispatchQueue.foremost.async {
        let place = textField.endOfDocument
        textField.selectedTextRange = textField.textRange(from: place, to: place)
    }
}

Is there a greater resolution to set the cursor on the final place?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments