iOS - UIWebView html 图片自适应屏幕

import "NewsDetailV.h"

@interface NewsDetailV()
<
UIWebViewDelegate
>


/** 整个crrollV*/
@property (nonatomic,strong) UIScrollView *scroolV;
/**  名称label*/
@property (nonatomic,strong) UILabel *nameLabel;
/** 来源于*/
@property (nonatomic,strong) UILabel *originateLabel;
/** 时间*/
@property (nonatomic,strong) UILabel *timeLabel;
/** WebV*/
@property (nonatomic,strong) UIWebView *webView;
/** webv 的 H*/
@property (nonatomic,assign) CGFloat webH;
/** 加载视图*/
@property (nonatomic,strong) UIActivityIndicatorView *indicator;
/** 线*/
@property (nonatomic,strong) UIView *lineV;


@end
@implementation NewsDetailV

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self initUserinterface];
    }
    return self;
}
#pragma mark -- UIWebViewDelegate


-- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int height = [height_str floatValue];
    height += 10;
    self.webH = height;
    [self.indicator stopAnimating];
    [self layoutSubviews];

}
-- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    [self.indicator stopAnimating];
    [MBProgressHUD showSuccess:@"详情加载失败..." toView:nil];
    self.webView.hidden = YES;
    CGFloat wholeH = (JobDetailVTitleVH + HeightOfLabel(TextFont(14, six))  + HeightScreen(20, six));
    [[NSNotificationCenter defaultCenter]postNotificationName:JobDesriptionChangeHNoti object:@{@"h":@(wholeH)}];
    return;

}


#pragma mark -- 界面
-(void)initUserinterface{

    // 整个crrollV
    UIScrollView *scroolV = [[UIScrollView alloc]init];
    scroolV.backgroundColor = WhiteColor;
    [self addSubview:scroolV];
    self.scroolV = scroolV;
    
    // 名称label
    UILabel *nameLabel = MakeLabelWith(ImTextColor, Px36FontSize);
    nameLabel.numberOfLines = 0;
    [scroolV addSubview:nameLabel];
    self.nameLabel = nameLabel;

    // 来源于
    UILabel *originateLabel = MakeLabelWith(TextColor, Px24FontSize);
    originateLabel.numberOfLines = 0;
    [scroolV addSubview:originateLabel];
    self.originateLabel = originateLabel;
    
    // 时间
    UILabel *timeLabel = MakeLabelWith(LiTextColor, Px24FontSize);
    [scroolV addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    // 线
    UIView *lineV = MakeLineView(self.scroolV, LineColor);
    self.lineV = lineV;
    
    // WebV
    UIWebView *webView = [[UIWebView alloc]init];
    webView.backgroundColor = [UIColor clearColor];
    webView.scrollView.bounces = NO;
    webView.delegate = self;
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    webView.scrollView.showsVerticalScrollIndicator = NO;
    webView.scrollView.scrollEnabled = NO;
    webView.dataDetectorTypes = UIDataDetectorTypeNone;
    webView.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];
    [self.scroolV addSubview:webView];
    self.webView = webView;
    


}
#pragma mark -- 布局
-(void)layoutSubviews{

    [super layoutSubviews];
    // 整个crrollV
    self.scroolV.frame = CGRM(0, HomeMargin, S_W, self.height - HomeMargin);
    // 名称label
    [self.nameLabel sizeToFit];
    CGFloat nameLabelY = ( HeightMacroScreen(30));
    self.nameLabel.frame = CGRM(MainLeftMargin,nameLabelY, S_W - MainLeftMargin * 2, self.nameLabel.height);
    // 来源于
    CGFloat originateLabelY = (nameLabelY + self.nameLabel.height + HeightMacroScreen(42));
    // 时间
    [self.timeLabel sizeToFit];
    self.timeLabel.frame = CGRM(S_W - MainLeftMargin - self.timeLabel.width, originateLabelY, self.timeLabel.width, self.timeLabel.height);
    // 来源于
    [self.originateLabel sizeToFit];
    self.originateLabel.frame = CGRM(MainLeftMargin, originateLabelY, self.timeLabel.x - WidMacroScreen(10) - MainLeftMargin, self.originateLabel.height);
    // 线
    CGFloat lineVY;
    if (self.originateLabel.text != nil && self.originateLabel.text.length > 0) {
        lineVY = CGRectGetMaxY(self.originateLabel.frame) + HeightMacroScreen(30);
    }else{
        lineVY = CGRectGetMaxY(self.timeLabel.frame) + HeightMacroScreen(30);
    }
    self.lineV.frame = CGRM(MainLeftMargin, lineVY, S_W - MainLeftMargin * 2, HeightPtMacroScreen(1));
    
    // 职位描述WebV
    CGFloat webViewY =  CGRectGetMaxY(self.lineV.frame) + HeightMacroScreen(50);
    self.webView.frame = CGRM(MainLeftMargin / 2,webViewY, S_W - MainLeftMargin ,self.webH);

    self.scroolV.contentSize = CGSizeMake(S_W, CGRectGetMaxY(self.webView.frame));
}


#pragma mark -- setter
// 数据字典
-(void)setDic:(NSDictionary *)dic{

    _dic  = dic;
    NSDictionary *news = [dic getDictionaryForKey:@"news" defaultValue:nil];
    self.nameLabel.text = [news getStringValueForKey:@"title" defaultValue:nil];
    NSString *publisher = [news getStringValueForKey:@"publisher" defaultValue:nil];
    if (publisher != nil && publisher.length > 0) {
        self.originateLabel.text = [NSString stringWithFormat:@"【%@】",publisher];
    }
    NSString *create_time = [news getStringValueForKey:@"create_time" defaultValue:nil];
    if (create_time != nil && create_time.length >= 10) {
        self.timeLabel.text = [NSString stringWithFormat:@"%@.%@.%@",[create_time substringToIndex:4],[create_time substringWithRange:NSMakeRange(5, 2)],[create_time substringWithRange:NSMakeRange(8, 2)]];
    }

    NSString *content = [news getStringValueForKey:@"content" defaultValue:nil];
    NSString * WEB_STYLE ;
    /** 全局web样式 */
    if (IPHONE_5_S || IPHONE_4_S) {
        WEB_STYLE = @"";
    }else if (IPHONE_6_S){
        WEB_STYLE = @"";
    }else{
        WEB_STYLE = @"";
    }
    
    NSString * resultHtml = [NSString stringWithFormat:@"%@%@",WEB_STYLE ,content];
    // 过滤掉 img标签的width,height属性
    NSString *replaceWidth = @"(]*?)\\s+width\\s*=\\s*\\S+";
    NSString *replaceHeight = @"(]*?)\\s+height\\s*=\\s*\\S+";
    
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:
                                              
                                              replaceWidth options:0 error:nil];
    NSRegularExpression *regularExpression2 = [NSRegularExpression regularExpressionWithPattern:
                                               
                                               replaceHeight options:0 error:nil];
    resultHtml  = [regularExpression stringByReplacingMatchesInString:resultHtml options:0 range:NSMakeRange(0, resultHtml.length) withTemplate:@"$1"];
    resultHtml  = [regularExpression2 stringByReplacingMatchesInString:resultHtml options:0 range:NSMakeRange(0, resultHtml.length) withTemplate:@"$1"];
    [self.webView loadHTMLString:resultHtml baseURL:nil];


    
    [self layoutSubviews];
}


#pragma mark -- getter

-(UIActivityIndicatorView *)indicator{

    if (!_indicator) {
        _indicator =  [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        //3.1 位置
        _indicator.center = CGPointMake(S_W / 2, S_H / 2);
        //3.2 关闭隐藏
        _indicator.hidesWhenStopped = YES;
        [_indicator startAnimating ];
    }
    return _indicator;

}


@end

你可能感兴趣的:(iOS - UIWebView html 图片自适应屏幕)