Font sizes in UIWebView does NOT match iOS font size(UIWebView 中的字体大小与 iOS 字体大小不匹配)
问题描述
这是一个 UILabel,上面写着关于".在 iOS 中设置为 17.7.
在它下面是一个 UIWebView,它也写着关于".也使用 css 设置为 17.7.
它们不匹配.
如何正确解决这个问题?
奇怪的是,在苹果自己的 UIWebView 中,大小基础不同?
要测试的 HTML...
<html><head><样式类型='文本/css'>身体 {边距:0px;字体系列:'SourceSansPro-Light';字体大小:FONTPOINTSpt;}html { -webkit-text-size-adjust:none;}</style></head><body leftmargin=0 topmargin=0>关于</body></html>
在设备或模拟器上的行为是相同的.
(注意,从
Here's a UILabel which says "About". Set at exactly 17.7 in iOS.
Below it a UIWebView which also says "About". Also set at exactly 17.7 using css.
They don't match.
How to fix this correctly?
It is bizarre that in Apple's own UIWebView, the size basis is different?
Html to test...
<html><head>
<style type='text/css'>
body {
margin: 0px;
font-family: 'SourceSansPro-Light';
font-size: FONTPOINTSpt;
}
html { -webkit-text-size-adjust:none; }
</style></head>
<body leftmargin=0 topmargin=0>
About
</body></html>
Behavior is identical on device or simulator.
(Note, from here I learned the ratio is, perhaps 72.0/96.0.)
If you want a 1 to 1 relationship between the font size that the UILabel
uses and the font size that the UIWebView
uses (via CSS) you have to use px
instead of pt
when defining the font size in your CSS.
Checkout this example HTML / CSS:
<html>
<head>
<style type='text/css'>
body {
font-family: 'SourceSansPro-Regular';
padding: 0
}
.point {
font-size: 17pt
}
.pixel {
font-size: 17px
}
</style>
</head>
<body>
<p class="point">About (17pt)<p>
<p class="pixel">About (17px)</p>
</body>
</html>
When you add a UIWebView
and a UILabel
to your UIViewController
and load the above HTML to the UIWebView
and set the same font to the UILabel
:
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: CGRect(x: 25, y: 50, width:325 , height: 90))
view.addSubview(webView)
let filePath = NSBundle.mainBundle().URLForResource("test", withExtension: "html")
webView.loadRequest(NSURLRequest(URL: filePath!))
let label = UILabel(frame: CGRect(x: 25, y: 150, width: 325, height: 40))
label.font = UIFont(name: "SourceSansPro-Regular", size: 17)
label.text = "About (UILabel set to 17)"
view.addSubview(label)
}
You get the following output:
这篇关于UIWebView 中的字体大小与 iOS 字体大小不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView 中的字体大小与 iOS 字体大小不匹配
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01