UIWebView加载html

来源:互联网 发布:剃须刀和刮胡刀知乎 编辑:程序博客网 时间:2024/05/24 07:07

#import <objc/runtime.h>


WebView.delegate = self;


Method origMethod = class_getInstanceMethod([WebView class], @selector(scrollViewDidZoom:));

Method newMethod = class_getInstanceMethod([WebView class], @selector(previousScrollViewDidZoom:));

method_exchangeImplementations(origMethod, newMethod);



NSString *WebViewText = @"";

NSString *htmlString = [WebViewText stringByAppendingFormat:@"%@", _model.content];

NSMutableString *htmlStr = [NSMutableString stringWithString:htmlString];

[htmlStr replaceOccurrencesOfString:@"<img" withString:@"<img onclick=window.location.href" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [htmlString length])];

[WebView loadHTMLString:htmlString baseURL:nil];



#pragma mark - WebView delegate

- (void)webViewDidFinishLoad:(UIWebView *)_webView
{
    UIScrollView *tmpScrollView = (UIScrollView *)[_webView.subviews objectAtIndex:0];
    NSLog(@"%@",NSStringFromCGSize(tmpScrollView.contentSize));
    CGRect rect = _webView.frame;
    rect.size.height = tmpScrollView.contentSize.height;
    _webView.frame = rect;
    
    [scrollView setContentSize:CGSizeMake(320, 220+tmpScrollView.contentSize.height)];//webView是加在scrollView上的
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        return NO;
    }
    return YES;
}

原创粉丝点击