views:

693

answers:

4

Hello,

is there a way of getting the position (CGPoint) of the cursor (blinking bar) in an UITextView (preferable relative to its content). I don’t mean the location as an NSRange. I need something around:

- (CGPoint)cursorPosition;

It should be a non-private API way.

Thanks, Raphael

A: 

There isn't a public method to get that information.

What are you trying to achieve? There might be another way.

Ben S
I try to mark a selected text, i.e. I receive a NSRange and want to draw a yellow rectangle behind that text. Is there another way?
Raphael Schaad
+1  A: 

Yes — as in there's a method to get the cursor position. Just use

CGRect caretRect = [textView rectContainingCaretSelection];
return caretRect.origin;

No — as in this method is private. There's no public API for this.

KennyTM
Exactly this method would be what I search, but I can't use private API of course. Other solution?
Raphael Schaad
+2  A: 

It's painful, but you can use the UIStringDrawing additions to NSString to do it. Here's the general algorithm I used:

CGPoint origin = textView.frame.origin;
NSString* head = [textView.text substringToIndex:textView.selectedRange.location];
CGSize initialSize = [head sizeWithFont:textView.font constrainedToSize:textView.contentSize];
NSUInteger startOfLine = [head length];
while (startOfLine > 0) {
    /*
     * 1. Adjust startOfLine to the beginning of the first word before startOfLine
     * 2. Check if drawing the substring of head up to startOfLine causes a reduction in height compared to initialSize.
     * 3. If so, then you've identified the start of the line containing the cursor, otherwise keep going.
     */
}
NSString* tail = [head substringFromIndex:startOfLine];
CGSize lineSize = [tail sizeWithFont:textView.font forWidth:textView.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
CGPoint cursor = origin;
cursor.x += lineSize.width;
cursor.y += initialSize.height - lineSize.height;
return cursor;

}

I used [NSCharacterSet whitespaceAndNewlineCharacterSet] to find word boundaries.

This can also be done (presumably more efficiently) using CTFrameSetter in Core Text, but that is not available in iPhone OS 3.1.3, so if you're targeting the iPhone you will need to stick to UIStringDrawing.

Tony
It's a nice workaround I also played around with.To find word boundaries I found it best to use CFStringTokenizer.Your implementation doesn't give the x and y coordinates but rather the size of the chars in the line containing the caret until the caret, right?However, I accept your approach as an answer since I eventually did something similar adopted to my needs.
Raphael Schaad
Thanks for the pointer about CFStringTokenizer, I overlooked that. Subtracting lineSize.height from initialSize.height will give you the y offset of the cursor (relative to the UITextView) and the width of the final line fragment gives you the x offset.
Tony
Can you elaborate more on/* * 1. Adjust startOfLine to the beginning of the first word before startOfLine * 2. Check if drawing the substring of head up to startOfLine causes a reduction in height compared to initialSize. * 3. If so, then you've identified the start of the line containing the cursor, otherwise keep going. */I also require cursor position in UITextView.
tek3
@tek3: initialSize is the total rectangle containing the whole string, but it doesn't tell you what x-coordinate the cursor is at in the last line. By chopping off words from the end and sizing the incrementally smaller string, you can find the index for the beginning of the last line. Then, using that, you can find the width of the last line up to the cursor (lineSize). (By the way, this is a really nice answer.)
Daniel Dickison
I think, to be honest *tek3* just wants to know what code to put inside the `while` function.
Joshua
A: 

I try to mark a selected text, i.e. I receive a NSRange and want to draw a yellow rectangle behind that text. Is there another way?

I can advise you some trick:

NSRange selectedRange = myTextView.selectedRange;
[myTextView select:self];
UIMenuController* sharedMenu = [UIMenuController sharedMenuController];
CGRect menuFrame = [sharedMenu menuFrame];
[sharedMenu setMenuVisible:NO];
myTextView.selectedRange = selectedRange

Using this code, you can know get the position of the cut/copy/past menu and there place your yellow rectangle.

I did not find a way to get the menu position witout forcing it to appear by a simulated select operation.

Regards Assayag

Meir Assayag
I implemented this for testing and it's a nice hack but actually not useful since you can't conclude from menuFrame to the cursor position (problems when arrow is pointing down and not up etc.).
Raphael Schaad