views:

17

answers:

1

I have used a UIWebView to simply show a string, with font size 14px (declared in style)

the string occupies approximately 1/4 - 1/3 of the iphone screen (in height) with restriction of width of 190

then I use NSString sizeWithFont: constrainedToSize: to detect how large is exactly this string.

UIFont *font = [UIFont fontWithName:@"ArialMT" size:14.0f];

CGSize titleConstraint = CGSizeMake(190, 440);

CGSize tSize = [s sizeWithFont:font constrainedToSize: titleConstraint lineBreakMode:UILineBreakModeWordWrap];

the tSize.height is printed out as 85

But actually I think it is more than 120, 1/4 - 1/3 of the screen height.

Why there is such a diff between the string size measurement and the actual UIWebView ?

the problem is the size of the UIFont is not the same as 14px? (in UIWebView, font size is 14px, in string sizeWithFont I used font size 14.0f)

If it is, how should I transfer html px font size to iPhone UIFont size?

thanks

+2  A: 

The sizeWithFont: method gives you the size that you would need if you were drawing the NSString directly. A UIWebView is going to put a margin around it, apply CSS stuff to it, and maybe do some magical "make it look nice for a web browser" algorithm before drawing.

I'm not sure there is any way you can calculate what size the UIWebView is going to use. I'd recommend one of these alternatives.

  • Don't use a UIWebView, if getting perfect placement is important. Implement your own text drawing code.
  • Just play with the size until it looks right. (It's not clear why you need to calculate this size, or whether the text can change.)
Kristopher Johnson
Thanks, yeah, i need to try different values so the UIFont size matches UIWebView style font size px. thanks again.
Jack