【问题标题】:How to change the styles of existing HTML string in swift?如何快速更改现有 HTML 字符串的样式?
【发布时间】:2017-10-11 03:41:43
【问题描述】:

我从 Web 服务获取 HTML 字符串并将其加载到 `UIWebView.一个示例 html 字符串是这样的。

" <div class='wg-rs-workAnniversary'> <div class='greetingtext'><span class='years work-years'>1 <sup>-</sup></span> <span class='event'>Work <br> Anniversary</span></div> <input type='hidden' class='hid-work-years' value='1'/> </div> <div class='wg-rs-btext work-years'>ABC Employee celebrates his 1<sup>-</sup> work anniversary today</div>"

加载后它只是一个黑色文本。我想增加这些文本的大小,更改颜色并将其放在UIWebView 上。我怎样才能快速做到这一点?请帮帮我。

【问题讨论】:

  • 我认为你应该增加这些文本的大小,改变 HTML 的颜色和中心。然后在 UIWebView 上加载它。

标签: html ios swift uiwebview


【解决方案1】:

你可以通过在你的 html 字符串中添加样式来做到这一点。

let htmlText = " <div class='wg-rs-workAnniversary'> <div class='greetingtext'><span> class='years work-years'>1 <sup>-</sup></span> <span> class='event'>Work <br> Anniversary</span></div> <input type='hidden'> class='hid-work-years' value='1'/> </div> <div class='wg-rs-btext> work-years'>ABC Employee celebrates his 1<sup>-</sup> work anniversary> today</div>"   
let htmlTextWithStyle = htmlText + ("<style>body{font-family: '\(UIFont.systemFont(ofSize: 12.0))'; font-size:\(14.0);}</style>")

要居中,您可以在 html 字符串中添加以下内容

"<style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;padding: 20px;text-align: center;-webkit-text-size-adjust: none;}</style>"

【讨论】:

  • 谢谢。我会试试这个。你能告诉我如何让它成为 UIWebView 的中心吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
相关资源
最近更新 更多