2017-10-11 24 views
0

我從Web服務獲取HTML字符串並將其加載到UIWebView上。一個示例html字符串是這樣的。如何在swift中更改現有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上。我怎樣才能迅速做到這一點?請幫幫我。

+0

下面我想你應該增加這些文本的大小,改變顏色和中心HTML。然後在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>" 
+0

謝謝。我會試試這個。你能告訴我如何使它成爲UIWebView的中心嗎? – das

+0

查看我編輯的答案,以查看您的內容。 –

+0

希望它能幫助你... –

相關問題