iOS WKWebView设置文字和图片大小

  WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];

    WKPreferences *preferences = [[WKPreferences alloc]init];

    preferences.javaScriptCanOpenWindowsAutomatically = true;

//    图片自适应高度

    NSString *jSString = @"var objs = document.getElementsByTagName('img');for(var i=0;i++){var img = objs[i];img.style.maxWidth = '100%';img.style.height='auto';}";

    WKUserScript *wkUserScript = [[WKUserScript alloc]initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

//    修改文字大小

    NSString*jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

    WKUserScript *wkScript = [[WKUserScript alloc]initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

    WKUserContentController *ucontroller = [[WKUserContentController alloc]init];

    [ucontroller addUserScript:wkUserScript];

    [ucontroller addUserScript:wkScript];

    configuration.preferences = preferences;

 

    

    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 10512) configuration:configuration];

    [self.view addSubview:wkWebView];

    self.edgesForExtendedLayout = UIRectEdgeNone;

    NSString * htmlString = self.memory;

    self.view.backgroundColor = [UIColor whiteColor];

    wkWebView.scrollView.bounces = NO;

    self.tableView.bounces = NO;

    wkWebView.navigationDelegate = self;

    [wkWebView loadHTMLString:htmlString baseURL:nil];

    self.wkWebView = wkWebView;

 

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {

    __block CGFloat webViewHeight=0;

    //获取内容实际高度(像素)@"document.getElementById(\"content\").offsetHeight;"

    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {

        // 此处js字符串采用scrollHeight而不是offsetHeight是因为后者并获取不到高度,看参考资料说是对于加载html字符串的情况下使用后者可以,但如果是和我一样直接加载原站内容使用前者更合适

        //获取页面高度,并重置webview的frame

        webViewHeight = [result doubleValue];

        NSLog(@"%f",webViewHeight);

        dispatch_async(dispatch_get_main_queue(), ^{

            if (webViewHeight != webView.bzHeight) {

                //高度重置

                webView.bzHeight =webViewHeight*2;

                _scrollView.bzHeight = webViewHeight*2;

                NSLog(@"%f",_webView.width);

                [self.contentLin layoutIfNeeded];

                [self.contentLin setNeedsLayout];

            }

            self.wkWebView.height = webViewHeight;

 

        });

    }];

    

    NSLog(@"结束加载");

}

//获取内容高度

    [wkWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    if ([keyPath isEqualToString:@"contentSize"]) {

        

        CGSize fitSize = [self.wkWebView sizeThatFits:CGSizeZero];

        self.wkWebView.frame = CGRectMake(0, 0, fitSize.width, fitSize.height);

        [self.tableView beginUpdates];

        [self.tableView setTableHeaderView:self.wkWebView];

        [self.tableView endUpdates];

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值