英文:
Why WebKit font vs iOS UILabel has different Font?
问题
以下是您要翻译的内容:
我正在使用webview,并且要更改`WKWebKit`的字体大小(50号),我正在使用以下代码:
`let fontSetting = "<span style=\"font-size: \(fontSize)\"></span>"`
我从Storyboard中获取了字体大小为50的`UILabel`。
为什么两者显示不同?
[![在此输入图片描述][1]][1]
我的完整代码如下:
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200))
webView.navigationDelegate = self
view.addSubview(webView)
}
override func viewDidAppear(_ animated: Bool) {
let htmlStr = ""
"<p><a href=\"https://www.google.com\">WebView-Button-size-50</a></p>"
""
let fontSize = 50
let fontSetting = "<span style=\"font-size: \(fontSize)\"></span>"
webView.loadHTMLString(fontSetting + htmlStr, baseURL: nil)
webView.allowsBackForwardNavigationGestures = true
}
}
<details>
<summary>英文:</summary>
I am using webview & for changing font(size 50) of `WKWebKit` i am using
`let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"`
I have taken `UILabel` from storyboard of font-size 50
Why both size shows different ?
[![enter image description here][1]][1]
My complete code is below ---
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200))
webView.navigationDelegate = self
view.addSubview(webView)
}
override func viewDidAppear(_ animated: Bool) {
let htmlStr = """
<p><a href="https://www.google.com">WebView-Button-size-50</a></p>
"""
let fontSize = 50
let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
webView.loadHTMLString(fontSetting + htmlStr, baseURL: nil)
webView.allowsBackForwardNavigationGestures = true
}
}
[1]: https://i.stack.imgur.com/1vAQ4.png
</details>
# 答案1
**得分**: 1
这是因为UILabel采用了默认字体(表示已分配字体),但对于web,您没有指定字体名称。
请检查下面的代码,它在我的端口上正常工作。
```swift
func adjustHTMLData(htmlText: String) {
let innerFontName: String = "myFontName".localized()
let innerFontSize1: CGFloat = 32 * iPhoneFactorX
let innerDirection: String = "myDir".localized()
var header002: String = htmlText
header002 = header002.replacingOccurrences(of: "font-family", with: "f ont-family")
header002 = header002.replacingOccurrences(of: "<font", with: "<ffont")
let htmlString: String = "<html><head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 10px;padding: 0px;font-family: '\(innerFontName)'; font-size: \(innerFontSize1)px;color:\(AppColors.color_000000);}</style></head><body dir='\(innerDirection)'>\(header002)<br /><br /><style>@font-face {font-family: '\(innerFontName)';font-weight: normal;src: url('\("fontTTF".localized())');}</style><style type='text/css'> iframe { width: 100% !important; } img {width: 100% !important; height: auto;} a {text-decoration: none; color : \(AppColors.color_000000)} table {width: 100% !important;} }</style></body></html>";
print("htmlString==\(htmlString)")
self.myWebview.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
}
在本地化中,我有以下内容:
"fontTTF"="Poppins-Regular.ttf";
"myFontName"="Poppins-Regular";
"myDir"="LTR";
AppsColor有以下内容:
static let color_000000: String = "#000000"
希望这能帮助到您。
英文:
This is because UILabel took default font (means font is assigned) however for web, you didn't specify font name.
Check below code which is working fine at my end.
func adjustHTMLData(htmlText : String) {
let innerFontName: String = "myFontName".localized()
let innerFontSize1: CGFloat = 32*iPhoneFactorX
let innerDirection: String = "myDir".localized()
var header002: String = htmlText
header002 = header002.replacingOccurrences(of: "font-family", with: "f ont-family")
header002 = header002.replacingOccurrences(of: "<font", with: "<ffont")
let htmlString: String = "<html><head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 10px;padding: 0px;font-family: '\(innerFontName)'; font-size: \(innerFontSize1)px;color:\(AppColors.color_000000);}</style></head><body dir='\(innerDirection)'>\(header002)<br /><br /><style>@font-face {font-family: '\(innerFontName)';font-weight: normal;src: url(\("fontTTF".localized()));}</style><style type='text/css'> iframe { width: 100%% !important; } img {width: 100% !important; height: auto;} a {text-decoration: none; color : \(AppColors.color_000000)} table {width: 100%% !important;} }</style></body></html>";
print("htmlString==\(htmlString)")
self.myWebview.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
}
In localization I have
"fontTTF"="Poppins-Regular.ttf";
"myFontName"="Poppins-Regular";
"myDir"="LTR";
AppsColor have below
static let color_000000 : String = "#000000"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论