iOS程序猿ios实用开发技巧iOS开发(OC)

cell加载html文本(计算UIWebView、WKWebVi

2018-10-30  本文已影响53人  重驹

.h文件

    //
    //  XBWCSGoodsDetailContentInfoCell.h
    //  wcs
    //
    //  Created by 刘飞 on 2018/8/2.
    //  Copyright © 2018年 ahxb. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    typedef void (^ReloadBlock)(void);
    @interface XBWCSGoodsDetailContentInfoCell : UITableViewCell

    @property(nonatomic,copy)NSString *htmlString;
    @property(nonatomic,copy)ReloadBlock reloadBlock;
    +(CGFloat)cellHeight;

    @end

.m文件

//
//  XBWCSGoodsDetailContentInfoCell.m
//  wcs
//
//  Created by 刘飞 on 2018/8/2.
//  Copyright © 2018年 ahxb. All rights reserved.
//  商品详情详情内容区域cell

#import "XBWCSGoodsDetailContentInfoCell.h"
#import <WebKit/WebKit.h>

@interface XBWCSGoodsDetailContentInfoCell()<UIWebViewDelegate,UIWebViewDelegate>//WKUIDelegate,WKNavigationDelegate>

@property (nonatomic,strong)UIWebView *webview;
@end

static CGFloat staticheight = 0;
@implementation XBWCSGoodsDetailContentInfoCell


+(CGFloat)cellHeight
{
    return staticheight;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    [self.contentView addSubview:self.webview];
}
return self;
}


 -(void)setHtmlString:(NSString *)htmlString
{
_htmlString = htmlString;

if (isEmptyStr(_htmlString)) {
    staticheight = 0;
    if (_reloadBlock) {
        _reloadBlock();
    }
}else{
    [self.webview loadHTMLString:[self reSizeImageWithHTML:_htmlString] baseURL:nil];
}

}
 -(void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
self.webview.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
self.webview.hidden = NO;
if (staticheight != height+1) {
    staticheight = height+1;
    if (staticheight > 0) {
        if (_reloadBlock) {
            _reloadBlock();
        }
    }
}
}
-(UIWebView *)webview
{
if (!_webview) {
    _webview =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];//这里一定要写1,不然高度不准确
    _webview.userInteractionEnabled = NO;
    _webview.hidden = YES;
    _webview.delegate = self;
}
return _webview;
}



/*
 *  让html文本适应屏幕
 */
-(nullable NSString *)reSizeImageWithHTML:(NSString *)html {
return [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%fpx}</style>%@", SCREEN_WIDTH-SIZE(15), html];//-SIZE(15)是为了html文本距离右边一定的间距,不然太紧贴边缘了
}



@end

上面是UIWebView加载html文本的方法,如果换成WKWebView将.m文件换成下面的

//
//  LFWKWebViewShowHtmlCell.m
//  CommonToolDemo
//
/ /  Created by 刘飞 on 2018/11/3.
//  Copyright © 2018年 ahxb. All rights reserved.
//

#import "XBWCSGoodsDetailContentInfoCell.h"
#import <WebKit/WebKit.h>

@interface XBWCSGoodsDetailContentInfoCell ()<WKUIDelegate,WKNavigationDelegate>
@property(nonatomic,strong)WKWebView * wkWebView;
@end

static CGFloat staticheight = 0;
@implementation XBWCSGoodsDetailContentInfoCell
+(CGFloat)cellHeight
{
    return staticheight;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.wkWebView];
}
return self;
}

-(WKWebView *)wkWebView{
if (!_wkWebView) {
    _wkWebView = [[WKWebView alloc] init];
    
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    wkWebConfig.userContentController = wkUController;
    wkWebConfig.userContentController = wkUController;
    _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1) configuration:wkWebConfig];
    _wkWebView.navigationDelegate = self;
    _wkWebView.userInteractionEnabled = NO;
    _wkWebView.UIDelegate = self;
    _wkWebView.scrollView.scrollEnabled = NO;
}
return _wkWebView;
}
-(void)setHtmlString:(NSString *)htmlString{
_htmlString = htmlString;
NSString *jSString =  [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%fpx}</style>%@", [UIScreen mainScreen].bounds.size.width-15, _htmlString];
[self.wkWebView loadHTMLString:jSString baseURL:nil];
}
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{

}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
__weak typeof(self)bself = self;
[webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id data, NSError * _Nullable error) {
    CGFloat height = [data floatValue];
    //ps:在WKWebView中前者offsetHeight获取自己加载的html片段,高度获取是相对准确的,但是若是加载的是原网站内容,用这个获取,会不准确,改用document.body.scrollHeight之后就可以正常显示,这个情况是我尝试了很多次方法才正常显示的
    CGRect webFrame = webView.frame;
    webFrame.size.height = height;
    webView.frame = webFrame;
    if (staticheight != height+1) {
        staticheight = height+1;
        if (staticheight > 0) {
            if (bself.reloadBlock) {
               bself.reloadBlock();
            }
        }
    }
}];


}
@end

ViewController中设置cell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return [XBWCSGoodsDetailContentInfoCell cellHeight];
}

ViewController中创建 cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(bself);
XBWCSGoodsDetailContentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XBWCSGoodsDetailContentInfoCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.htmlString = isEmptyStr(self.goodsDetailModel.content)?@"":self.goodsDetailModel.content;
    cell.reloadBlock =^(void)
    {
        [bself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}

为了加载的流畅度,建议还是用WKWebView,内存消耗上讲也会比UIWebView好很多
Demo集合

上一篇下一篇

猜你喜欢

热点阅读