【发布时间】:2017-01-15 00:05:03
【问题描述】:
我正在将文本加载到 WKWebView 中。我的目标是:
- 将字体大小设置为固定值
- 在 WKWebView 中垂直对齐文本
- 在 WKWebView 中水平对齐文本
我无法在 WKWebView 上找到可以做到这一点的函数。所以我试图将字符串包装成一些 HTML 代码。
这是我的代码:
func loadQuestions() {
questionView.questionLabel.text = questionArray[questionIndex].questionText
let questionText: String = questionArray[questionIndex].questionText!
let answerText: String = questionArray[questionIndex].answerText!
let htmlWrap = "<p>\(questionText)</p><p>\(answerText)</p>"
answerView.webView.loadHTMLString(htmlWrap, baseURL: nil)
}
这可行,但是当我尝试添加一些使用转换器网站获得的高级格式时(因为我不知道 html),我得到了所有使我的字符串无效的引号 (")。
let htmlWrap = "<p style="text-align: center;"><span style="font-size: 36pt;">\(questionText)</span></p><p style="text-align: center;"><span style="font-size: 36pt;">\(answerText)</span></p>"
上面的不会编译。
似乎我可以在每个引号(“)之前在html字符串中使用转义字符(\),如果它是一条简单的行,这似乎可以工作。但不适用于复杂的html。那我该怎么做这个?
谢谢!
【问题讨论】:
标签: html css swift swift3 wkwebview