Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to set text color in UIWebView?

I'm using the method loadHTMLString:baseURL to place content in a UIWebView.

I'd like to set the text color of the text shown in the UIWebView.

I've searched around and found two things mentioned: 1) I need to modify the html string by adding html code to set the color. 2) I saw something about NSAttributedStrings, but I haven't figure out how to use this yet.

I don't know html, so I'd prefer to avoid it if possible.

1 Answer

Hi Fred,

You can easily change the color of the text by wrapping the HTML into a div tag with an inline style.

<div style="color:#cc0000">...your html...</div>
NSString *finalHTML = [NSString stringWithFormat:@"%@%@%@", @"<div style=\"color:#cc0000\">", initialHTML, @"</div>"];

Thanks. That worked. However, as I mentioned, I don't know html. Is there a similar line of code to set the color via RGB values?

Found answer to my last question.

Replace the #cc0000 method to specify color with rgb(191,195,207) as shown at http://www.w3schools.com/tags/att_font_color.asp.

Yep, that will work as well, didn't cross my mind to specify it also in the first place.