Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

text position in UITextView

How can i get position of special word in UIText View in iphone SDK ?

Intel, Mac OS X (10.6.2)

Posted on Jul 19, 2010 3:09 AM

Reply
1 reply

Jul 19, 2010 3:55 PM in response to sajjadzare

I don't think there's any direct method for determining the x-y coordinates of a particular run of text (whether selected or not) in UITextView. If I needed to do this, I would use an iterative algorithm. E.g., a simplified algorithm, which assumes the target text is one word, which can't be broken and can be rendered on a single line, might go something like this:

1) Find the range of the target in the full text using [rangeOfString:options:range:|http://developer.apple.com/iphone/library/documen tation/Cocoa/Reference/Foundation/Classes/NSString Class/Reference/NSString.html#//appleref/occ/instm/NSString/rangeOfString:options:range:];

2) From the end of the range (1), get the substring of the full text that ends in the target using [substringToIndex:|http://developer.apple.com/iphone/library/documentation/Coco a/Reference/Foundation/Classes/NSString Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-substringToIndex];

3) Get the bounding rectangle of the substring (2) using [sizeWithFont:constrainedToSize:lineBreakMode:|http://developer.apple.com/iphon e/library/documentation/UIKit/Reference/NSString UIKit_Additions/Reference/Reference.html#//appleref/doc/uid/TP40006893-CH3-SW9];

4) Remove words from the end of the substring (2) until the height of the bounding rectangle (3) is reduced. The length of the shortened substring should give you the index at the start of the line which contains the target; Save the reduced bounding rectangle as rUpper;

5) Get the bounding rectangle of the line which begins at the start index (4), rLine, as well as the bounding rectangle of the target, rTarget, using [sizeWithFont:constrainedToSize:|http://developer.apple.com/iphone/library/docu mentation/UIKit/Reference/NSString UIKit_Additions/Reference/Reference.html#//appleref/doc/uid/TP40006893-CH3-SW8];

6) The target bounding rectangle, r, relative to the bounds of the text view object should then be obtained as:
r.origin.x = rLine.size.width - rTarget.size.width;
r.origin.y = rUpper.size.height;
r.size.width = rTarget.size.width;
r.size.height = rTarget.size.height;

\- Ray

text position in UITextView

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.