1. webview 控件封裝
@implementation MineWebBkg
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self drawView];
}
return self;
}
-(void)drawView{
self.bkgView = [[UIView alloc]init];
self.bkgView.userInteractionEnabled = YES;
self.bkgView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.bkgView];
[self.bkgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.height.mas_equalTo(1);
make.left.mas_equalTo(self.mas_left);
make.right.mas_equalTo(self.mas_right);
}];
//圖文詳情
if (!_webView) {
self.webView = [UIWebView new];
[self.bkgView addSubview:self.webView];
}
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
self.webView.dataDetectorTypes = UIDataDetectorTypeNone;//添加目的去掉可以點擊鏈接(但是網絡連接沒有實現www.xxxx.html)
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.bkgView);
make.top.mas_equalTo(self.bkgView.mas_top);
make.height.mas_equalTo(1);
}];
}
//請求成功後自適應高度
- (void)webViewDidFinishLoad:(UIWebView *)webView{
/*
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
_webView.scalesPageToFit = YES;
*/
// CGFloat width_scr = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue];
// if(width_scr==0){
// width_scr = SCREEN_WIDTH-12*2;
// }
// CGFloat scale = width_scr/(SCREEN_WIDTH-12*2);
//
// CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue]/scale;
CGSize size = [webView sizeThatFits:CGSizeZero];
[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
webView.scrollView.scrollEnabled = NO;
webView.scrollView.showsVerticalScrollIndicator = NO;
webView.scrollView.showsHorizontalScrollIndicator = NO;
[self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.bkgView.mas_top);
make.left.right.mas_equalTo(self.bkgView);
make.height.mas_equalTo(size.height);
}];
[self.bkgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.height.mas_equalTo(size.height);
make.left.mas_equalTo(self.mas_left);
make.right.mas_equalTo(self.mas_right);
}];
}
2.加載:
//加載HTML字符串內容完成圖文混編
[self.infoBkg.webView loadHTMLString:HTML內容 baseURL:[NSURL URLWithString:HTML裏面省略的頭]];