在UIWebView中更改文本颜色

时间:2013-05-24 12:48:30

标签: objective-c uiwebview

我正在UIWebView中加载Word文档。我可以将文档的文本颜色设置为黑色背景吗?

我使用此代码添加黑色背景,但文本颜色为灰色(不是白色):

[self.view setBackgroundColor:[UIColor blackColor]];
[self.webView setOpaque:NO];
[self.webView setBackgroundColor:[UIColor clearColor]];

3 个答案:

答案 0 :(得分:1)

您可以使用此代码更改UIWebView中的文字颜色和backgorund颜色。

- (void)webViewDidFinishLoad:(UIWebView *)_webView{

    [self.webView setOpaque:NO];
    NSString *setJavaScript = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white';document.getElementsByTagName('div')[0].style.backgroundColor='black'; DOMReady();"];
    [self.webView stringByEvaluatingJavaScriptFromString:setJavaScript];
}

答案 1 :(得分:0)

使用十六进制颜色

[self.itemSummary loadHTMLString:[NSString stringWithFormat:@"<html><body bgcolor=\"#000000\" text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@</body></html>", [item objectForKey:@"description"]] baseURL: nil];

http://www.color-hex.com/

答案 2 :(得分:0)

UIWebView显示方法与显示网页相同。因此,要调整显示,您可以使用CSS。

例如,您可以使用CSS显示白色的所有文本:

body { color: #FFFFFF;}

所以只需添加包含在样式标记中的CSS规则:

NSString *stringWithWhiteFont = [NSString stringWithFormat:@"<style>body { color: #FFFFFF;}</style>%@", css, htmlStringToBeDisplayed];
[webView stringWithWhiteFont baseURL:nil];

我不确定如何在UIWebView中显示word文档。看来你需要使用UIDocumentController打开word文档。

UIWebView does not open ms word (doc) and ms excel (xls) files

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009304