Change textcolor of UIWebView
Asked Answered
B

6

9

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?

Barsky answered 2/7, 2012 at 10:16 Comment(3)
Is there any way to do programatically ? because on some pages i want text color as black and in some pages i want text color as white.Barsky
will have a problem if you color your text using css? did you mean you want to change the text color of ONE SINGLE .html black or white?Revelry
i want to change color of my html file programtically. there are lots of html pages, in some html pages i want text color as white and in some pages i want text color as black, tht's why i want to change text color.Barsky
V
15

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];
Vane answered 2/7, 2012 at 10:23 Comment(9)
i am loading html file locally from my machine like this, [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];Barsky
what is changeColor in your EDIT ? i have not added any class like changeColor in my cssBarsky
are you use css or local htmlVane
ya, i am loading file from my local machine,[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]]; where _pagesPath is /Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/first.html here first.html contains text of black color, i want to change that text color as white.Barsky
@ketansahasrabudhe, please drag this.html in your projectVane
i am getting SIGABRT here [self._webview loadRequest:[NSURLRequest requestWithURL:[NS.... Barsky
[self._webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"first" ofType:@"html"]isDirectory:NO]]];Vane
ya i wrote, first instead of test still SIGABRTi think error is in nsbundleBarsky
dude you rock you helped me a lot it working now.. thank you so muchBarsky
S
5
[_webview loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: white; font-size: 17; font-family: HelveticaNeue; color: #ffffff\">%@</body></html>", strDataFromHTML] baseURL: nil]; 
Shortcut answered 2/7, 2012 at 12:11 Comment(1)
dude i am reading a html file from my simulator path, like /Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/first.html and i don't know, what will be the content inside first.htmlBarsky
S
1

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];
Stores answered 30/1, 2014 at 15:41 Comment(1)
Thanks, stole your code and made a category from it :)Sclerenchyma
C
1

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
    }
}
Cupo answered 20/8, 2015 at 7:48 Comment(1)
Hello, Can you tell me how can I load my string to web view after passing this extentionHomopolar
T
0

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

Timothy answered 2/7, 2012 at 10:23 Comment(0)
T
-1

try this...

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

Hope this helps.

Trichiasis answered 2/7, 2012 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.