Change textcolor of UIWebView

2019-04-22 20:37发布

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?

6条回答
时光不老,我们不散
2楼-- · 2019-04-22 21:17

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-22 21:22
[_webview loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: white; font-size: 17; font-family: HelveticaNeue; color: #ffffff\">%@</body></html>", strDataFromHTML] baseURL: nil]; 
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-04-22 21:37

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];
查看更多
冷血范
5楼-- · 2019-04-22 21:37

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];
查看更多
何必那么认真
6楼-- · 2019-04-22 21:40

try this...

  1. Parse the html file .
  2. Show the contents as per your requirements.

Hope this helps.

查看更多
SAY GOODBYE
7楼-- · 2019-04-22 21:41

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
    }
}
查看更多
登录 后发表回答