问题
I am making an epub reader, into which I am loading HTML pages in my webview
:
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];
Now I want to change background color and and text color of my the HTML pages. I changed background color of my webview using,
self._webview.backgroundColor = [UIColor blackColor];
self._webview.opaque = NO;
That's working, but I also want to change the text color of my webview. How do I do this?
回答1:
In this this code color:#fff
tag use for text color #fff use black color
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 the 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];
回答2:
[_webview loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: white; font-size: 17; font-family: HelveticaNeue; color: #ffffff\">%@</body></html>", strDataFromHTML] baseURL: nil];
回答3:
I made a method that wraps some text in a HTML body with the correct style for a given UIColor
and UIFont
.
Simply pop 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];
回答4:
Here is a small Swift Extension to handle 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
}
}
回答5:
try to add the code in your HTML file what ever you are getting from server... then only you can change the text color.... or add some HTML code before your HTML file for changing the color of text
回答6:
try this...
- Parse the html file .
- Show the contents as per your requirements.
Hope this helps.
来源:https://stackoverflow.com/questions/11291689/change-textcolor-of-uiwebview