Home > Web Front-end > HTML Tutorial > iOS中通过设置CSS改变WebView字体大小,颜色,背景颜色_html/css_WEB-ITnose

iOS中通过设置CSS改变WebView字体大小,颜色,背景颜色_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:38:23
Original
1725 people have browsed it

因为WebView自带的属性是不可以改变字体大小颜色这些需求的,只能通过改变css样式,内置来解决。在百度上搜了半天很多不靠谱,现在给出几种解决方案如下:

1.第一种方式:在WebView的代理方法webViewDidFinishLoad中:

字体大小: [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '12%'"];

字体颜色:[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'green'"];

背景颜色:[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#F6F7F3'"];

2.第一种方式:在下面可以通过改变margin大小来改变webview文字距离边距大小,13代表字体大小,self.strBookTips是要被操作的字符串。

    NSString *BookStr = [NSString stringWithFormat:@" \n"

                         "

\n"

                         "

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

                         " \n"

                         " \n"

                         "

%@ \n"

                         "",13,self.strBookTips];

[self.webView loadHTMLString:BookStr baseURL:nil];

3.第三种方式,这种缺少了改变背景颜色

    NSString * formatString = @"%@";

    NSString * htmlString = [NSString stringWithFormat:formatString,self.strBookTips];

[self.webView loadHTMLString:BookStr baseURL:nil];



版权声明:本文为博主原创文章,未经博主允许不得转载。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template