开发者

Pixel-Position of Cursor in UITextView

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.


Requires iOS 5

CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;

Remember to check that selectedTextRange is not nil before calling this method. You should also use selectedTextRange.empty to check that it is the cursor position and not the beginning of a text range. So:

if (textview.selectedTextRange.empty) {
    // get cursor position and do stuff ...
}


SWIFT 4 version:

if let cursorPosition = textView.selectedTextRange?.start {
    // cursorPosition is a UITextPosition object describing position in the text (text-wise description)

    let caretPositionRectangle: CGRect = textView.caretRect(for: cursorPosition)
    // now use either the whole rectangle, or its origin (caretPositionRectangle.origin)
}

textView.selectedTextRange?.start returns a text position of the cursor, and we then simply use textView.caretRect(for:) to get its pixel position in textView.


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 CoreText, 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.


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.


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


Take a screenshot of the UITextView, then search the pixel data for colors that match the color of the cursor.

-(CGPoint)positionOfCursorForTextView:(UITextView)textView {
     //get CGImage from textView
     UIGraphicsBeginImageContext(textView.bounds.size);
     [textView.layer renderInContext:UIGraphicsGetCurrentContext()];
     CGImageRef textImageRef = UIGraphicsGetImageFromCurrentImageContext().CGImage;
     UIGraphicsEndImageContext();  

     //get raw pixel data
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     uint8_t * textBuffer = (uint8_t*)malloc(Width * Height * 4);
     NSUInteger bytesPerRow = 4 * Width;
     NSUInteger bitsPerComponent = 8;
     CGContextRef context = CGBitmapContextCreate(textBuffer, Width, Height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);


     CGColorSpaceRelease(colorSpace);

     CGContextDrawImage(context, CGRectMake(0, 0, Width, Height), textImageRef);
     CGContextRelease(context);

     //search 

     for(int y = 0; y < Height; y++)
     {
         for(int x = 0; x < Width * 4; x += 4)
         {
             int red   = textBuffer[y * 4 * (NSInteger)Width + x];
             int green = textBuffer[y * 4 * (NSInteger)Width + x + 1];    
             int blue  = textBuffer[y * 4 * (NSInteger)Width + x + 2];  
             int alpha   = textBuffer[y * 4 * (NSInteger)Width + x + 3];    


             if(COLOR IS CLOSE TO COLOR OF CURSOR)
             {
                 free(textBuffer);
                 CGImageRelease(textImageRef);
                 return CGPointMake(x/4, y);
             }
         }
     }

     free(textBuffer);
     CGImageRelease(textImageRef);
     return CGPointZero;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜