如何在ios中为AepubReader添加日夜模式

时间:2013-03-25 08:08:22

标签: ios xcode epub

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

1.Night模式仅影响特定章节。 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];
  //    }

2 个答案:

答案 0 :(得分: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];

}}

答案 1 :(得分: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];
  }
}