如何更改loadedHtml文本中的字体颜色

Had*_*adi 1 html xcode objective-c

我保存了这样的字符串..

NSString *loadString = [NSString stringWithFormat:@"<html><body><font color=\"white\">%@</font></body></html>", string];
Run Code Online (Sandbox Code Playgroud)

现在我想在webview中加载此字符串

[webView loadHTMLString:loadString baseURL:nil];
Run Code Online (Sandbox Code Playgroud)

现在加载网页时,颜色默认为黑色.我想将颜色更改为白色,但我无法这样做.任何建议如何更改字体颜色.谢谢.

编辑1:我试过这个,但没有运气..

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];
Run Code Online (Sandbox Code Playgroud)

这也..

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];
Run Code Online (Sandbox Code Playgroud)

Nin*_*ino 5

这是一个完整的清单.只需创建一个名为"WebViewTestViewController"的视图控制器+ NIB,并在您的nib文件中添加webView并连接它.

WebViewTestViewController.h:

#import <UIKit/UIKit.h>

@interface WebViewTestViewController : UIViewController {
    UIWebView *webView;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


小智 5

我希望您在UIWebview中要求更改已加载文件的字体颜色,您可以使用以下步骤在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\""];
Run Code Online (Sandbox Code Playgroud)

在这里,我使用javascript来更改我的插座引用的html页面正文的字体系列,大小和颜色 IBOutlet UIWebView *web;