iPhone 中奇怪的字符串大小检测
我使用 UIWebView 来简单地显示一个字符串,字体大小为 14px(在样式中声明),
该字符串大约占据 iphone 屏幕的 1/4 - 1/3(高度),宽度限制为 190,
然后我使用 NSString sizeWithFont :constrainedToSize:检测这个字符串到底有多大。
UIFont *font = [UIFont fontWithName:@"ArialMT" size:14.0f];
CGSize titleConstraint = CGSizeMake(190, 440);
CGSize tSize = [s sizeWithFont:字体 constrainedToSize: titleConstraint lineBreakMode:UILineBreakModeWordWrap];
tSize.height 打印出来为 85
但实际上我认为它超过 120,屏幕高度的 1/4 - 1/3。
为什么字符串大小测量和实际 UIWebView 之间存在如此大的差异?
问题是 UIFont 的大小与 14px 不一样? (在UIWebView中,字体大小为14px,在字符串sizeWithFont中我使用字体大小14.0f)
如果是,我应该如何将html px字体大小传输到iPhone UIFont大小?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sizeWithFont:
方法为您提供直接绘制 NSString 时所需的大小。 UIWebView 将在其周围留出边距,对其应用 CSS 内容,并且可能在绘制之前执行一些神奇的“使其在 Web 浏览器中看起来不错”算法。我不确定有什么方法可以计算 UIWebView 将使用的大小。我推荐其中一种替代方案。
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.