iOS webView加载本地html 调用 js,css (基本使用)

首先加载本地的html文件:

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"post.dark" ofType:@"html"];
        NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        [_javaWebView loadHTMLString:html baseURL:baseURL];

然后调用webView的dai里方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView

[webView stringByEvaluatingJavaScriptFromString:JSstringAction];

可以用这个方法向js文件进行传值,调用js方法,给js方法设置参数:

实例应用:

html:

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="app.light.css" type="text/css"></link>
    <script src="zepto.min.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>
</head>
<body>
    <div id="title">
<!--       四色丛林迷彩减肥睡?-->
    </div>
    <div id="author_avatar">
<!--    <p><img src="http://bbs.junpinzhi.com/uc_server/avatar.php?size=middle&uid=13931"/>  军品志</p>-->
    </div>
    <div id="page_header"></div>
    <div id="content">
    //传入内容
        </div>
    <div id="page_footer"></div>
    <div id="page_bottom"></div>
</body>

</html>

js部分 :

function initBody1(para) {
    var pageContent = $("#content");
    pageContent.html(para);
    pageContent.css('font-size','100% ');
    
}

function initBody2(para) {
    var pageContent = $("#content");
    pageContent.html(para);
    pageContent.css('font-size','120% ');
}

css:部分

#content {
    color: #1a1a1a;
    font-size: 100%;
    margin: 0 1em;
    line-height: 150%;
    letter-spacing: 50%;
    word-wrap: break-word;
    word-break:normal;
/*    white-space: pre-wrap;*/
    text-align:justify;
}
#content p {
    margin: 1em auto;
}
#content img {
    /* keep aspect ratio, use max width */
    max-width:100%;
    height:auto;
    /* center image */
    margin:1em auto;
    display: block;
}
#content .img_desc {
    color:#888888;
    font-size:75%;
    line-height:120%;
    margin-bottom:0.4em;
}

可以对css的"#content"做修改  达到想要的效果  


我们通过

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"initBody2('%@')",self.titleStr]];
向js文件传送content 在传到html     在掉用css进行改变.


t#content {

    color: #1a1a1a;

    font-size: 100%;

    margin: 0 1em;

    line-height: 150%;

    letter-spacing: 50%;

    word-wrap: break-word;

    word-break:normal;

/*    white-space: pre-wrap;*/

    text-align:justify;

}

#content p {

    margin: 1em auto;

}

#content img {

    /* keep aspect ratio, use max width */

    max-width:100%;

    height:auto;

    /* center image */

    margin:1em auto;

    display: block;

}

#content .img_desc {

    color:#888888;

    font-size:75%;

    line-height:120%;

    margin-bottom:0.4em;

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值