1

目前我正在使用 AepubReader 读取 epub 文件,但我想在其中实现日夜模式?你知道我该如何实现这些模式吗?目前我正在使用以下代码,但问题是

1.夜间模式仅影响特定章节。2.Day模式将字体颜色更改为黑色。

 NSUserDefaults *userDefaults2 = [NSUserDefaults standardUserDefaults];
  [userDefaults2 setBool:NO forKey:@"cc"];
  [userDefaults2 synchronize];

  [webView setOpaque:NO];
  [webView setBackgroundColor:[UIColor blackColor]];
  //    for(int i=0;i<10;i++)
  //    {
  NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('html')[0].style.webkitTextFillColor= 'white'"];

  [webView stringByEvaluatingJavaScriptFromString:jsString];
  //    }
4

2 回答 2

2

这段代码对我有用

-(IBAction)night:(id)sender{

NSUserDefaults *userDefaults2 = [NSUserDefaults standardUserDefaults];
[userDefaults2 setBool:NO forKey:@"btnM1"];
[userDefaults2 synchronize];

[_webview setOpaque:NO];
[_webview setBackgroundColor:[UIColor blackColor]];
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
[_webview stringByEvaluatingJavaScriptFromString:jsString];}

-(IBAction)day:(id)sender{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"btnM1"];
[userDefaults synchronize];

[_webview setOpaque:NO];
[_webview setBackgroundColor:[UIColor whiteColor]];
NSString *jsString2 = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
[_webview stringByEvaluatingJavaScriptFromString:jsString2];}





- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSUserDefaults *menuUserDefaults = [NSUserDefaults standardUserDefaults];

if([menuUserDefaults boolForKey:@"btnM1"]){
    [_webview setOpaque:NO];
    [_webview setBackgroundColor:[UIColor whiteColor]];
    NSString *jsString2 = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
    [_webview stringByEvaluatingJavaScriptFromString:jsString2];

}

else{
    [_webview setOpaque:NO];
    [_webview setBackgroundColor:[UIColor blackColor]];
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
    [_webview stringByEvaluatingJavaScriptFromString:jsString];

}}
于 2013-03-26T02:38:35.063 回答
0
  • 您也可以将此代码用于日夜模式。此代码可能会根据您的需要工作。

  • 这将适用于UIWebView.

代码是:

BOOL isNightMode;

- (IBAction)nightModeBtn_click:(id)sender {
    isNightMode = YES;
    [self.webView reload];
}
- (IBAction)dayModeBtn_click:(id)sender {
    isNightMode = NO;
    [self.webView reload];
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView{
  if(isNightMode == YES){
    [self.webView setOpaque:NO];
    NSString *setJavaScript = [[NSString alloc] initWithFormat:@" document.getElementsByTagName('body')[0].style.webkitFilter='grayscale(100%%)';document.getElementsByTagName('div')[0].style.webkitFilter='grayscale(100%%)'; DOMReady();"];
    [self.webView stringByEvaluatingJavaScriptFromString:setJavaScript];
  }
}
于 2017-10-31T10:11:30.483 回答