Lavrin Pristazh
Lavrin Pristazh

Reputation: 131

Disable setting cursor in the middle of a link in a text view

I'd like to disable editing links in a text view, that's why I need to disable setting cursor in the middle of the link. The video link demonstrates what I want to accomplish: https://www.youtube.com/watch?v=uOOC9A93kn8

Any ideas how to achieve that are much appreciated

Upvotes: 0

Views: 137

Answers (1)

Lalo
Lalo

Reputation: 438

You can get the position of the cursor in a UITextField like this:

if let selectedTextRange = textField.selectedTextRange {
    let cursorPosition = textField.offset(from: textField.beginningOfDocument,
                                          to: selectedTextRange.start)
}

You can offset the cursor by doing something like this:

let offset = 2
if let positionWithOffset = textField.position(from: textField.beginningOfDocument,
                                               offset: offset) {
    textField.selectedTextRange = textField.textRange(from: positionWithOffset,
                                                      to: positionWithOffset)
}

So you can use that in textField(_:shouldChangeCharactersIn:replacementString:) from UITextFieldDelegate to achieve the behavior you want by offsetting the cursor to the beginning of the link.

Upvotes: 1

Related Questions