3

I use the following code to move the cursor position to 5 characters from the beginning of a UITextField:

 txtView.selectedRange = NSMakeRange(5, 0);

Now, if I have a cursor at an arbitrary position as shown in the image below, how can I move the cursor up, down, left and right?

enter image description here

Kamil
  • 968
  • 1
  • 8
  • 18
  • 1
    Right & left, that's easy, it's just that instead of `NSMakeRange(something, 0);` something +1 or -1 (be careful of the bounds). For up/down, that's more tricky, because you have to know the line, the current range according to the position in that line. Maybe some possible leads there: https://stackoverflow.com/questions/17557845/uitextview-get-the-current-line – Larme Mar 05 '18 at 12:46
  • maybe this one helps : https://stackoverflow.com/questions/15931227/uitextfield-set-cursor-to-start-text-position – Nirav Kotecha Mar 05 '18 at 12:50
  • @NiravKotecha No, the problem here is different. – Kamil Mar 05 '18 at 12:52
  • @Larme Ok, I will implement it myself then. I was hoping to find a more elegant solution. – Kamil Mar 05 '18 at 12:56
  • Add your current scenario (Detail process) so others can understand well and they will you help you :) – Ravi Panchal Mar 05 '18 at 13:38

1 Answers1

2

Left and right should be more or less easy. I guess the tricky part is top and bottom. I would try the following.

You can use caretRect method to find the current "frame" of the cursor:

if let cursorPosition = answerTextView.selectedTextRange?.start {
    let caretPositionRect = answerTextView.caretRect(for: cursorPosition)


}

Then to go up or down, I would use that frame to calculate estimated position in UITextView coordinates using characterRange(at:) (or maybe closestPosition(to:)), e.g. for up:

let yMiddle = caretPositionRect.origin.y + (caretPositionRect.height / 2)
let lineHeight = answerTextView.font?.lineHeight ?? caretPositionRect.height // default to caretPositionRect.height
// x does not change
let estimatedUpPoint = CGPoint(x: caretPositionRect.origin.x, y: yMiddle - lineHeight)
if let newSelection = answerTextView.characterRange(at: estimatedUpPoint) {
    // we have a new Range, lets just make sure it is not a selection
    newSelection.end = newSelection.start

    // and set it
    answerTextView.selectedTextRange = newSelection
} else {
    // I guess this happens if we go outside of the textView
}

I haven't really done it before, so take this just as a general direction that should work for you.

Documentation to the methods used is here.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90