zoukankan      html  css  js  c++  java
  • 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;">为自己拼接的样式部分,用于两端对齐
  • 相关阅读:
    windows向Linux服务器上传、下载,服务器内复制、移动文件
    解决:ubuntu提示E: 无法获得锁 /var/lib/dpkg/lock-frontend
    PostgreSQL安装与简单操作
    LeetCode 234.回文链表
    LeetCode 445.两数相加 II
    LeetCode 24.两两交换链表中的节点
    LeetCode 19.删除链表的倒数第 n 个节点
    LeetCode 26.删除排序数组中的重复项
    Java 程序运行机制
    String 属于基础的数据类型吗?
  • 原文地址:https://www.cnblogs.com/wusang/p/9223688.html
Copyright © 2011-2022 走看看