tags:

views:

147

answers:

2

Hi I am new as a Iphone developer. I am wondering how to get the length of a given string under one specific UIFont, WITHOUT line word wrap.

say I have a NSString* lpText = @"a unknon lenght string ..... string" //could be very long and I am using the "Times New Rome" font for rendering...

Any idea? Thanks in advance.

+1  A: 

This should work (untested):

CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

Set the font you need. The line break mode shouldn't matter here.

Paul Lynch
thanks, it works fine.
semix
+1  A: 

Look up the UIKit additions to NSString:

  • (CGSize)sizeWithFont:(UIFont *)font Returns the size of the string if it were to be rendered with the specified font on a single line.

  • (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size Returns the size of the string if it were rendered and constrained to the specified size.

  • (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

Returns the size of the string if it were rendered with the specified constraints.

Kenny