How can I add an external stylesheet to a UIWebView in Xcode?

前端 未结 4 1808

I have looked at various similar questions and answers and still cannot get this to work, so I\'m adding my own question:

I\'m playing with UIWebView. I can create a

4条回答
  •  猫巷女王i
    2020-12-14 09:36

    I thought I should post the entire code block to access an external CSS file. Hopefully, it will be useful to others:

    - (void)viewDidLoad
    {
        NSURL *mainBundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"mypagename" ofType:@"html"];
        NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    
        NSString *htmlString = [[NSString alloc] initWithData: 
                                  [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
        webView.opaque = NO;
        webView.backgroundColor = [UIColor clearColor];
        [self.webView loadHTMLString:htmlString baseURL:mainBundleURL];
    
        [htmlString release];
    }
    

提交回复
热议问题