webView如何调用css,js文件

1.看下面代码

ViewController.h 文件

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UIWebViewDelegate>

{

    UIWebView *iWebView;

}

-(void)loadDocument:(NSString *)docName;

@end

 

ViewController.m文件

 

#import "ViewController.h"

 

@implementation ViewController

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

}

//加载本地使用说明文件文件

-(void)loadDocument:(NSString *)docName 

{

    NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];

    NSString *path=[mainBundleDirectory stringByAppendingPathComponent:docName];

    

    NSURL *url=[NSURL fileURLWithPath:path];

    NSURLRequest *request=[NSURLRequestrequestWithURL:url];

    iWebView.scalesPageToFit=YES;

    [iWebView loadRequest:request];

}

#pragma mark - View lifecycle

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    iWebView = [[UIWebViewalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    [self.view addSubview:iWebView];

    [iWebViewsetBackgroundColor:[UIColorclearColor]];

    

    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    [header setBackgroundColor:[UIColorclearColor]];

    

    [selfloadDocument:@"1.html"];

  //[selfloadDocument:@"my.html"]; 

//    CGRect browserCanvas = CGRectMake(0,0,320,416);

//    for (int i = 0; i<[iWebView.scrollView.subviews count]; i++) {

//        UIView *subview = [[iWebView.scrollView subviews] objectAtIndex:i];

//        

//        CGRect f = subview.frame;

//        NSLog(@"sub %d -> x:%.0f, y:%.0f, w:%.0f, h:%.0f", i, f.origin.x, f.origin.y, f.size.width, f.size.height);

//        

//        if(f.origin.x == browserCanvas.origin.x && 

//           f.origin.y == browserCanvas.origin.y && 

//           f.size.width == browserCanvas.size.width && 

//           f.size.height == browserCanvas.size.height)

//        {

//            f.origin.y = header.frame.size.height;

//            subview.frame = f;

//        }

//    }

    //[iWebView.superview insertSubview:header atIndex:0];

    //[(UIScrollView *)[iWebView.subviews objectAtIndex:0] setContentInset:UIEdgeInsetsMake(50, 0, 0, 0)];

   // [iWebView conformsToProtocol:<#(Protocol *)#>]

   // [iWebView insertSubview:header atIndex:0];

//    if (!header) {

//        <#statements#>

//    }

// Do any additional setup after loading the view, typically from a nib.

}

 

- (void)viewDidUnload

{

    [superviewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}

 

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}

 

- (void)viewWillDisappear:(BOOL)animated

{

[superviewWillDisappear:animated];

}

 

- (void)viewDidDisappear:(BOOL)animated

{

[superviewDidDisappear:animated];

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

 

@end

 

  1.html文件

<html> 

    <head>

    <title>My Awesome Page</title>

    <link rel="stylesheet"href = "test.css"type="text/css"/>

    <script type = "text/javascript"src = "222.js"></script>

    </head>

    <body>

        <h1 id="foo"onclick ="sayHello()">Click me!</h1>

    </body> 

</html>

 

  222.js文件

function sayHello() {

    if (document.getElementById('foo').innerHTML == 'Hi there'){

        document.getElementById('foo').innerHTML = 'Click me!';

        

    }else{

        document.getElementById('foo').innerHTML = 'Hi there'

    }

}

  test.css 文件

body{

    color:red;

    background-color:#808080;

    font-size:22px;

    font-style:italic;

    font-weight:bold;

    font-family:Arial;

}

  my.html文件 

<html>

    <head>

        <title>MyAwesome Page</title>

        <link rel="stylesheet"href = "test.css"type="text/css"/>

    </head>

    <body>

        <h1 class="loud" >Hi there!</h1>

        <p id="highlight">Thanks for visiting my web page.</p>

        <p>I hope you like it.</>

        <ul>

            <li class="loud">Pizza</li>

            <li>Beer</li>

            <li>Dogs</li>

        </ul>

    <body>

</html>

 

2.另外webView通过url 加载页面

 

UIWebView *webView;

 

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 480.0-44*2-20);

UIWebView *webViewTemp = [[UIWebView alloc] initWithFrame:webFrame];

self.webView = webViewTemp;

[webViewTemp release];

 

 

[self.webViewsetBackgroundColor:[UIColorwhiteColor]];

self.webView.delegate =self;

self.webView.scalesPageToFit = YES;

NSURL *url = [NSURL URLWithString:self.urlStr];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:requestObj];

[self.viewaddSubview:self.webView];

 

原文地址:https://www.cnblogs.com/jiangshiyong/p/2537711.html