当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS获取cell中webview的内容尺寸

iOS获取cell中webview的内容尺寸

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近项目中遇到在cell中获取webview的内容的尺寸的需求 实现的思路其实很简单 就是通过执行js 获取尺寸即可 为了后面用着方便我直接封装了一个html的cell 起名就叫 

sthtmlbasecell 下面是实现代码:

#import "stbasetableviewcell.h"@class sthtmlbasecell;
@protocol sthtmlbasedelegate <nsobject>
- (void)webviewdidload:(sthtmlbasecell *)cell height:(cgfloat)height;
@end
@interface sthtmlbasecell : stbasetableviewcell
@property (weak, nonatomic) id<sthtmlbasedelegate>delegate;

@end

以上是.h文件的实现 很简单 就是声明了 sthtmlbasecell  然后创建了代理 这个代理方法 就是返回给外部webview的内容的高度的 

#import "sthtmlbasecell.h"

@interface sthtmlbasecell()<uiwebviewdelegate>

@property (weak, nonatomic) iboutlet uiwebview *webview;@end

@implementation sthtmlbasecell

- (void)awakefromnib {
  [super awakefromnib];
  // initialization code
  self.webview.scrollview.scrollenabled = no;
  self.webview.scrollview.pagingenabled = no;
  self.webview.delegate = self;
  self.webview.backgroundcolor = [uicolor whitecolor];
}
- (void)configcellwithhtml:(nsstring *)html //外界传入的html字符串
{
  [self.webview loadhtmlstring:html baseurl:nil];//加载html
}

#pragma mrak - uiwebviewdelegate
- (bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)navigationtype
{
  
  [webview stringbyevaluatingjavascriptfromstring:@"document.documentelement.style.webkituserselect='auto';"];//让用户可以选中webview里面内容
  [webview stringbyevaluatingjavascriptfromstring:@"document.documentelement.style.webkittouchcallout='auto';"];//可以响应用户的手势
  
  nsurl *url = [request url];
  if (![url host]) {
    return yes;
  }
 return no;
}

- (void)webviewdidfinishload:(uiwebview *)webview
{
  cgfloat height = [[webview stringbyevaluatingjavascriptfromstring:
             @"document.body.scrollheight"] floatvalue]; //获取webview内容的高度
  self.webview.height = height;
  if ([self.delegate respondstoselector:@selector(webviewdidload:height:)]) {
    [self.delegate webviewdidload:self height:height];//调用代理的方法 
  }
}


@end

大致就这么简单  就能够在cell中获取webview 的内容尺寸了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网