How to find a pixel-positon of a cursor in UITextView?

前端 未结 2 1728
小鲜肉
小鲜肉 2021-01-01 01:35

I\'m developing a simple writing app for iPad.

I\'m trying to compute the pixel-position of the cursor in UITextView. I spend a few weeks to design this

2条回答
  •  独厮守ぢ
    2021-01-01 01:46

    If you target to IOS7 only you could use UITextView method:

    - (CGRect)caretRectForPosition:(UITextPosition *)position;
    

    Short sample:

    NSRange range; // target location in text that you should get from somewhere, e.g. textview.selectedRange
    UITextView textview; // the text view
    
    UITextPosition *start = [textview positionFromPosition:textview.beginningOfDocument offset:range.location];
    CGRect caretRect = [self caretRectForPosition:start]; // caret rect in UITextView
    

提交回复
热议问题