Change text color of UIWebView

I make an epub reader into which I load HTML pages into my webview :

 [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]]; 

Now I want to change the background color and text color of my HTML pages. I changed the background color of my web browser using

 self._webview.backgroundColor = [UIColor blackColor]; self._webview.opaque = NO; 

This works, but I also want to change the text color of my web view. How to do it?

+7
source share
6 answers

In this case, this color:#fff tag color:#fff used for the text color. #Fff black color is used.

 NSString *webStr =@ "Your text use"; [self._webview loadHTMLString:[NSString stringWithFormat:@"<div id ='foo' align='left' style='line-height:18px; float:left;width:300px; font-size:13px;font-family:helvetica;background-color:transparent; color:#fff;>%@<div>",webStr] baseURL:nil]; 

if you use local html use

  NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"]; NSString* text = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",htmlFile); NSLog(@"Hello"); [self._webview loadHTMLString:[NSString stringWithFormat:@"<html><body bgcolor=\"#000000\" text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@</body></html>", text] baseURL: nil]; 
+10
source
 [_webview loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: white; font-size: 17; font-family: HelveticaNeue; color: #ffffff\">%@</body></html>", strDataFromHTML] baseURL: nil]; 
+5
source

I made a method that wraps some text in an HTML body with the correct style for the given UIColor and UIFont .

Just enter the generated HTML into the webview:

 NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>" textFont:[UIFont systemFontOfSize:10] textColor:[UIColor grayColor]]; [webView loadHTMLString:htmlString baseURL:nil]; 
+1
source

Here is a small quick extension for this:

 import UIKit extension String { func htmlFormattedString(font:UIFont, color: UIColor) -> String { var numberOfColorComponents:Int = CGColorGetNumberOfComponents(color.CGColor) var colorComponents = CGColorGetComponents(color.CGColor) var colorHexString = "" if numberOfColorComponents == 4 { var red = colorComponents[0] * 255 var green = colorComponents[1] * 255 var blue = colorComponents[2] * 255 colorHexString = NSString(format:"%02X%02X%02X", Int(red), Int(green), Int(blue)) as String } else if numberOfColorComponents == 2 { var white = colorComponents[0] * 255 colorHexString = NSString(format:"%02X%02X%02X", Int(white), Int(white), Int(white)) as String } else { return "Color format noch supported" } return NSString(format: "<html>\n <head>\n <style type=\"text/css\">\n body {font-family: \"%@\"; font-size: %@; color:#%@;}\n </style>\n </head>\n <body>%@</body>\n </html>", font.familyName, String(stringInterpolationSegment: font.pointSize), colorHexString, self) as String } } 
+1
source

try adding code to your HTML file, no matter what you get from the server ... then you can change the color of the text .... or add HTML code before the HTML file changes the color of the text

0
source

try it...

  • Parse the html file.
  • Show content according to your requirements.

Hope this helps.

0
source

All Articles