UITextPosition to Int
Asked Answered
H

2

14

I have a UISearchBar on which I am trying to set a cursor position. I am using UITectField delegates as I couldn't find anything direct for UISearchBar. Below is the code I am using:

  UITextField *textField = [searchBar valueForKey: @"_searchField"];
  // Get current selected range , this example assumes is an insertion point or empty selection
  UITextRange *selectedRange = [textField selectedTextRange];
  // Construct a new range using the object that adopts the UITextInput, our textfield
  UITextRange *newRange = [textField textRangeFromPosition:selectedRange.start toPosition:selectedRange.end];

Question is in the 'newRange' object for 'toPosition' I want to have something like selectedRange.end-1; as I want the cursor to be on second last position.

How do I set the cursor to second last position?

Histology answered 14/10, 2013 at 21:15 Comment(0)
K
10

the clue is to make a position and then a range with no length and then select it

e.g.

- (IBAction)select:(id)sender {
    //get position: go from end 1 to the left
    UITextPosition *pos = [_textField positionFromPosition:_textField.endOfDocument
                                               inDirection:UITextLayoutDirectionLeft
                                                    offset:1];

    //make a 0 length range at position
    UITextRange *newRange = [_textField textRangeFromPosition:pos
                                                toPosition:pos];

    //select it to move cursor
    _textField.selectedTextRange = newRange;
}
Kloof answered 14/10, 2013 at 21:30 Comment(0)
B
34

Swift 5

I came across this question originally because I was wondering how to convert UITextPosition to an Int (based on your title). So that is what I will answer here.

You can get an Int for the current text position like this:

if let selectedRange = textField.selectedTextRange {
    // cursorPosition is an Int
    let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
}

Note: The properties and functions used above are available on types that implement the UITextInput protocol so if textView was a UITextView object, you could replace the instances of textField with textView and it would work similarly.

For setting the cursor position and other related tasks, see my fuller answer.

Betweenwhiles answered 21/1, 2016 at 12:15 Comment(1)
This is probably the worst designed API I've used so far. Why wouldn't they just make a property for it since they literally say in the docs the only reason they do it like this is because WebKit needs an object. Thanks Apple.Bipetalous
K
10

the clue is to make a position and then a range with no length and then select it

e.g.

- (IBAction)select:(id)sender {
    //get position: go from end 1 to the left
    UITextPosition *pos = [_textField positionFromPosition:_textField.endOfDocument
                                               inDirection:UITextLayoutDirectionLeft
                                                    offset:1];

    //make a 0 length range at position
    UITextRange *newRange = [_textField textRangeFromPosition:pos
                                                toPosition:pos];

    //select it to move cursor
    _textField.selectedTextRange = newRange;
}
Kloof answered 14/10, 2013 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.