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.
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;
}
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With