Change webview font size, color, background color by setting CSS in iOS

Source: Internet
Author: User

Because the WebView property is not changing the font size color of these requirements, only by changing the CSS style, built-in to solve. Baidu on the search for a long time a lot of not reliable, now gives several solutions as follows:

1. The first way: In the WebView proxy method Webviewdidfinishload:

Font size: [WebView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. style.webkittextsizeadjust= ' 12% ' "];

Font color:[WebView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. Style.webkittextfillcolor= ' green ' "];

Background color:[WebView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. style.background= ' #F6F7F3 ' "];

2. The first way: in the following can change the margin size to change the WebView text distance margin size, 13 represents the fontsize, self. Strbooktips is the string to be manipulated.

nsstring *bookstr = [nsstringstringwithformat:@ "

"

"<style type=\" text/css\ "> \ n"

"Body {margin:10;font-size:%d;} \ n "

"</style> \ n"

"

"<body>%@</body> \ n"

",self. Strbooktips];

[Self. WebView loadhtmlstring: Bookstr baseURL:nil];

3. The third way, this lack of changing the background color

nsstring * formatString =@ "<span style=\" Font-size:13px;color: #7E7C8A \ ">%@</ Span> ";

   nsstring * htmlstring = [nsstring stringwithformat : Formatstring,self . strbooktips ];

[ self webview &NBSP; loadhtmlstring :BOOKSTR&NBSP; baseurl : nil ";



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Change webview font size, color, background color by setting CSS in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.