How to change the starting cursor position inside UITextView

454 views Asked by At

I have a uitextview which become the first responder on viewload. but i want to change the position of the cursor of the uitextview at certain position in x for the first 2 line only.

2

There are 2 answers

0
Avt On

Maybe you could use a UITextKit (a very good tutorial).

For example to have a rounded text you can use something like:

UIBezierPath* exclusionPath = [UIBezierPath bezierPathWithOvalInRect:yourTextView.bounds];
exclusionPath = [exclusionPath bezierPathByReversingPath];
yourTextView.textContainer.exclusionPaths = @[exclusionPath];
0
Govindarao Kondala On

Have you tried following solutions ?

Controlling cursor position in a UITextField is complicated because so many abstractions are involved with input boxes and calculating positions. However, it's certainly possible. You can use the member function setSelectedTextRange:

[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];

Here's a function which takes a range and selects the texts in that range. If you just want to place the cursor at a certain index, just use a range with length 0:

+ (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
    UITextPosition *start = [input positionFromPosition:[input beginningOfDocument] 
                                                 offset:range.location];
    UITextPosition *end = [input positionFromPosition:start
                                               offset:range.length];
    [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}

For example, to place the cursor at idx in the UITextField input:

    [Helpers selectTextForInput:input 
                        atRange:NSMakeRange(idx, 0)];

Source: https://stackoverflow.com/a/11532718/914111

Have not tested due to some busy so please let us know wether it is working or not (May have issue in IOS8.0)