ios-oc-加载HTML字符串图片自适应尺寸大小

2022-08-17  本文已影响0人  马铃薯蜀黍
// 自适应尺寸大小
- (NSString *)autoWebAutoImageSize:(NSString *)html{
     
    NSString * regExpStr = @"<img\\s+.*?\\s+(style\\s*=\\s*.+?\")";
    NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:regExpStr options:NSRegularExpressionCaseInsensitive error:nil];
     
    NSArray *matches=[regex matchesInString:html
                                    options:0
                                      range:NSMakeRange(0, [html length])];
     
     
    NSMutableArray * mutArray = [NSMutableArray array];
    for (NSTextCheckingResult *match in matches) {
        NSString* group1 = [html substringWithRange:[match rangeAtIndex:1]];
        [mutArray addObject: group1];
    }
     
    NSUInteger len = [mutArray count];
    for (int i = 0; i < len; ++ i) {
        html = [html stringByReplacingOccurrencesOfString:mutArray[i] withString: [NSString stringWithFormat:@"style=\"width:%f; height:auto;\"",(k_SCREEN_WIDTH - 24)]];
    }
     
    return html;
}
上一篇下一篇

猜你喜欢

热点阅读