Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nxcoder

macrumors newbie
Original poster
Dec 29, 2012
2
0
hello

i want to get the size (width and height) of between begin and end of selected text . see this :

1a3cwtg3sumlzxc5v4sb.png


i am tried this code :

Code:
CGSize size=[myText sizeWithFont:txt.font];

but not worked . because i have not font informations.
Suppose that i have not the font size and font name of my text
for example , the height of the selected texts is 250 pixel .
help please

thanks
 
What is your end goal for the information you seek?

The method you are using only calculates the size if the text was on a single line. You need a method that returns the size for a multiline layout.

I've had some recent good luck with the sizeWithFont:constrainedToSize:lineBreakMode: method. This may be useful for you but it depends on your end goal. Tip: I set the height for the constrainedToSize value greater than I expect to be using.

You seem to be saying that you do not have the font information. The font information you are using is the txt.font property. So I don't know why you would be saying you don't have the information.

Here is a sample using the above method:
Code:
UIFont * myfont = [UIFont systemFontOfSize: 24.0];
NSString mystring = @"Some long string.";
CGSize aBoundingBox = {125, 500}; // Height greater than expected
CGSize boundingSize = [mystring sizeWithFont: myfont constrainedToSize: aBoundingBox lineBreakMode: NSLineBreakByWordWrapping];
 
if your on iOS6 then UITextView conforms to UITextInputProtocol. One of the required methods of the protocol is
Code:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
.

IS that the sort of information your looking for?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.