How can I change font color and font style of ePub

2019-03-21 07:28发布

I have created app of books which supports pdf and ePub format. In that app, pdf is just shown as an image and ePub open in webview because EPUB is just XHTML stored in a zipfile with an XML manifest. Now, I want to change the font style and font color of ePub book pages. Is it possible? If possible, then how can I achieve this? I have searched a lot, but did not get any proper solution.

1条回答
Luminary・发光体
2楼-- · 2019-03-21 08:18

I have found the solution. I have make a string and evaluate using Java string

This is what I have written:

NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);"                               // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
"}"
"}";

NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];

    // this is what change the text style 

NSString *insertRule3 = [NSString stringWithFormat:@"addCSSRule('html, body, div, p, span, a', 'font-family: arial;')"];
NSString *changeColor = [NSString stringWithFormat:@"addCSSRule('html, body, div, p, span, a', 'color: #1122CC;')"];

[webView stringByEvaluatingJavaScriptFromString:varMySheet];

[webView stringByEvaluatingJavaScriptFromString:addCSSRule];

[webView stringByEvaluatingJavaScriptFromString:insertRule1];

[webView stringByEvaluatingJavaScriptFromString:insertRule2];

[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

[webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule3];
    [webView stringByEvaluatingJavaScriptFromString:changeColor];
查看更多
登录 后发表回答