Recently, I encountered the need to get the size of the webView content in the cell. The implementation idea is actually very simple. It is to get the size by executing js. For the sake of convenience, I directly encapsulated an HTML cell and named it.
STHTMLBaseCell The following is the implementation code:
#import ""@class STHTMLBaseCell; @protocol STHtmlBaseDelegate <NSObject> - (void)webViewDidLoad:(STHTMLBaseCell *)cell height:(CGFloat)height; @end @interface STHTMLBaseCell : STBaseTableViewCell @property (weak, nonatomic) id<STHtmlBaseDelegate>delegate; @end
The above is the implementation of the .h file. It is very simple. It declares STHTMLBaseCell. Then creates a proxy. This proxy method is to return the height of the content of the external webView.
#import "" @interface STHTMLBaseCell()<UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView;@end @implementation STHTMLBaseCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code = NO; = NO; = self; = [UIColor whiteColor]; } - (void)configCellWithHtml:(NSString *)html //The html string passed from outside{ [ loadHTMLString:html baseURL:nil];//Load html} #pragma mrak - UIWebViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { [webView stringByEvaluatingJavaScriptFromString:@"='auto';"];//Let users select the content in the webview [webView stringByEvaluatingJavaScriptFromString:@"='auto';"];// Can respond to user gestures NSURL *url = [request URL]; if (![url host]) { return YES; } return NO; } - (void)webViewDidFinishLoad:(UIWebView *)webView { CGFloat height = [[webView stringByEvaluatingJavaScriptFromString: @""] floatValue]; //Get the height of the webview content = height; if ([ respondsToSelector:@selector(webViewDidLoad:height:)]) { [ webViewDidLoad:self height:height];//The method of calling the proxy } } @end
It's roughly that simple. You can get the content size of the webview in the cell.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.