iOS wkWebview調整html文字大小以及文字兩端對齊


1.問題:后台給的HTML標簽字符串用wkwebview加載時需要適當增大文字大小以及將文字兩端對齊

修改前效果:

修改后效果:

解決方法:文字增大在wk代理方法實現,使用js語句;文本兩端對齊使用添加css樣式實現

2.實現方法

代碼1:

#pragma mark -webView delegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    //禁止用戶選擇
    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
    // 適當增大字體大小
    [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '105%'" completionHandler:nil];
}

關鍵語句:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '105%'"

代碼2:

displayContent = [NSString stringWithFormat:@"<div style=\"text-align:justify; text-justify:inter-ideograph;\">%@",displayContent];
[self.disPlayWeb loadHTMLString:displayContent baseURL:nil];

關鍵語句:<div style="text-align:justify; text-justify:inter-ideograph;">   注:"在oc中需要轉義為\"

3.補充

后台返回的帶標簽文字完整示例:

<div style="text-align:justify; text-justify:inter-ideograph;"><p><span style="FONT-SIZE: 19px; FONT-FAMILY: &#39;Times New Roman&#39;,&#39;serif&#39;">Music is a part in our life, nearly everybody will listen to the music, no matter what nation they are from. Music is divided into many types. Once blues belonged to the black people, but as time went by, music is accepted by all classes. People always share the hottest songs together, just like they can communicate. Once in the foreign country, I had dinner in a restaurant, where there had the singers. People enjoyed the music when they were eating. At this moment, it was music that brought people together, and they share the same emotion.&nbsp;</span></p>
<div style="text-align:justify; text-justify:inter-ideograph;">為自己拼接的樣式部分,用於兩端對齊

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM