Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding (x,y) coordinate of specified word in UITextView

Here is my contents in UITextView...

==>"A ‘Metropolitan’ dining experience offering exquisite food prepared with attention to visual detail & taste."

Now my question is simple and short,how to find the location(x,y) of a word "experience" within the textview.

Any solution will be greatly appreciated.

like image 209
Master Stroke Avatar asked Jan 17 '13 09:01

Master Stroke


2 Answers

Try this:

- (CGPoint)getPoint:(UITextView *)textView
{
    NSRange range = [textView.text rangeOfString:@"experience"];

    UITextPosition *beginning = textView.beginningOfDocument;
    UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textView positionFromPosition:start offset:range.length];
    UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
    CGRect rect = [textView firstRectForRange:textRange];
    return rect.origin;
}
like image 85
Hsm Avatar answered Sep 22 '22 07:09

Hsm


Try this if you want in Swift

    let range: NSRange = (txtView.text as NSString).range(of: text)
    let beginning: UITextPosition? = txtView.beginningOfDocument
    let start: UITextPosition? = txtView.position(from: beginning!, offset: range.location)
    let end: UITextPosition? = txtView.position(from: start!, offset: range.length)
    let textRange: UITextRange? = txtView.textRange(from: start!, to: end!)
    let rect: CGRect = txtView.firstRect(for: textRange!)
like image 23
Pushp Avatar answered Sep 22 '22 07:09

Pushp