Skip to content

Instantly share code, notes, and snippets.

@jkatzer
Created February 24, 2013 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkatzer/5022357 to your computer and use it in GitHub Desktop.
Save jkatzer/5022357 to your computer and use it in GitHub Desktop.
lazy html/css background color passing to uiwebview. don't think this is necessary but hate for the code to go to waste. i wouldn't use it in a real project, but i just love playing with communication between uiwebview and the document it contains.
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString* backgroundColorString = [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.background"];
if ([backgroundColorString length] != 0) {
if ([backgroundColorString rangeOfString:@"rgb("].location == NSNotFound) {
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@"rgb(" withString:@""];
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@")" withString:@""];
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray* colors = [backgroundColorString componentsSeparatedByString:@","];
webView.backgroundColor = [UIColor colorWithRed:[colors[0] floatValue] green:[colors[1] floatValue] blue:[colors[2] floatValue] alpha:1.0];
} else if ([backgroundColorString rangeOfString:@"rgba("].location == NSNotFound) {
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@"rgba(" withString:@""];
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@")" withString:@""];
backgroundColorString = [backgroundColorString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray* colors = [backgroundColorString componentsSeparatedByString:@","];
webView.backgroundColor = [UIColor colorWithRed:[colors[0] floatValue] green:[colors[1] floatValue] blue:[colors[2] floatValue] alpha:[colors[3] floatValue]];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment