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

1.問題:後臺給的HTML標籤字符串用wkwebview加載時須要適當增大文字大小以及將文字兩端對齊css

修改前效果:web

修改後效果:ide

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

2.實現方法this

代碼1:lua

#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%'"spa

代碼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中須要轉義爲\"rest

3.補充code

後臺返回的帶標籤文字完整示例:

<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;">爲本身拼接的樣式部分,用於兩端對齊
相關文章
相關標籤/搜索