Home / iPhone / Adding CSS to UIWebView in iOS

Adding CSS to UIWebView in iOS

By: Simon
Updated:
Aug 6, 2013
Posted: Aug 6, 2013

css-uiwebview

Came across a couple of snippets I put together to add a CSS file to a web view. It seems like this would be easy, but it really wasn’t, at least for me. As a side note, I am iPhone app developer and I am still learning everyday. So here’s the snippet:

Put it in the viewDidLoad,

NSString *bodyHTML = [NSString stringWithFormat:@"<html> \n"
 "<head> \n"
 "<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\" /> \n"
 "</head> \n"
 "<body\">%@</body> \n"
 "</html>", webLink];
NSString *path = [[NSBundle mainBundle] bundlePath];
 NSURL *baseURL = [NSURL fileURLWithPath:path];
 [webView loadHTMLString:bodyHTML baseURL:baseURL];

Simon Urbina

Simon Urbina

Simon is a Product Designer and Front End Dev with over 20 years of experience. He started as a graphic designer and illustrator coding his first website in 1996. He has worked with brands like Publix, Microsoft, and Discovery Channel.