开发者

How to change font color in a loadedHtml text

开发者 https://www.devze.com 2023-03-14 14:37 出处:网络
I have save a string like this.. NSString *loadString = [NSString stringWithFormat:@\"<html><body><font color=\\\"white\\\">%@</font></body></html>\", string];

I have save a string like this..

NSString *loadString = [NSString stringWithFormat:@"<html><body><font color=\"white\">%@</font></body></html>", string];

Now i want to load this string in a webview using

[webView loadHTMLStrin开发者_开发技巧g:loadString baseURL:nil];

now when the webpage is loaded the colour is default as black. I want to change the colour to white but m unable to do so. any suggestion how can i change the font colour.Thanks.

EDIT 1: I tried this but no luck..

NSString *htmlString = [NSString stringWithFormat:@"<html><style type=\"text/css\"><!--.style1 { font-family: Arial;color: #FFFFFF;font-size: 20px;font-weight: bold;}--> <body><font class=\"style1\">%@</font></body></html>", string];

also this..

NSString *htmlString = [NSString stringWithFormat:@"<html> \n""<head> \n""<style type=\"text/css\"> \n""body {font-family: \"%@\"; font-size: %@; color:#FFFFFF; }\n""</style> \n""</head> \n""<body>%@</body> \n""</html>", @"helvetica", [NSNumber numberWithInt:15], string];


Here is a full listing. Just create a view controller + NIB called "WebViewTestViewController" and add the webView in your nib file and connect it.

WebViewTestViewController.h:

#import <UIKit/UIKit.h>

@interface WebViewTestViewController : UIViewController {
    UIWebView *webView;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@end

WebViewTestViewController.m:

#import "WebViewTestViewController.h"

@implementation WebViewTestViewController
@synthesize webView;

- (void)viewDidAppear:(BOOL)animated {
    NSString *string = @"Hello world";
    NSString *loadString = [NSString stringWithFormat:@"<html><head><style>body {background:blue} p {color:white;}</style></head><body><p>%@<p></body></html>", string];
    [webView loadHTMLString:loadString baseURL:nil];

    [super viewDidAppear:animated];
}

- (void)dealloc {
    [webView release];
    [super dealloc];
}


- (void)viewDidUnload {
    [self setWebView:nil];
    [super viewDidUnload];
}

@end


I hope you are asking on changing the font color of the loaded files in UIWebview,You can use the following steps to change the color,size and type of the loaded content in the UIWebview,

[web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.color =\"Green\""];


[web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.fontFamily =\"Courier New\""];
[web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.fontSize =\"20\""];

here i used the javascript to change the font family,size and color of the body of the html page referenced to my outlet IBOutlet UIWebView *web;


Before the body tag put this:

<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
font-size: 20px;
font-weight: bold;
}
-->

And then in your font tag include:

 <font class="style1">

If this doesn't work change font for span.

EDIT --- Here is an example...

<html><style type="text/css">.style1 {font-family: Arial, Helvetica, sans-serif;color: #FFFFFF;font-size: 20px;font-weight: bold;}</style><body><span class="style1">%@</span></body></html>", string];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号