美文网首页
ios-oc-加载HTML字符串图片自适应尺寸大小

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

作者: 马铃薯蜀黍 | 来源:发表于2022-08-17 10:23 被阅读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;
    }

    相关文章

      网友评论

          本文标题:ios-oc-加载HTML字符串图片自适应尺寸大小

          本文链接:https://www.haomeiwen.com/subject/krsjgrtx.html