5

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.

3
  • Similar to this stackoverflow.com/questions/8683848/… Hope its a same as what you wanted ! Commented Jan 17, 2013 at 10:20
  • @Reefaq..thanks rafeeq,but the solution is hardcoded..i am sure with the use some calculation logic we can achieve it. Commented Jan 17, 2013 at 10:27
  • 2
    play with the codes then you can achieve it .. you can take that link as a direction to get your solution. Commented Jan 17, 2013 at 10:47

3 Answers 3

3

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; } 
Sign up to request clarification or add additional context in comments.

Comments

3

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!) 

Comments

2

use sizeWithFont, it suppose to be something like this:

NSString *text = textView.text; NSString *substring = [text substringToIndex:[text rangeOfString:@"experience"].location]; CGSize size = [substring sizeWithFont:textView.font]; CGPoint p = CGPointMake((int)size.width % (int)textView.frame.size.width, ((int)size.width / (int)textView.frame.size.width) * size.height); 

8 Comments

i am getting some float value for p.x...what does that p.x meant for?
p.x suppose to be the x coordinate of the text you want in the whole view. [substring sizeWithFont:textView.font].width is the x coordinate in the textview
i am getting p.x=1368.000000,my UITextview width itself is 360.000.is there any calculation pending to find the exact (x,y) of the word?
does your UItextview is one liner or have couple of lines?
I edited my answer, it might fit for your multiline text view.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.