美文网首页
html代码处理(如图片、字体大小)

html代码处理(如图片、字体大小)

作者: 番薯大佬 | 来源:发表于2018-01-30 17:33 被阅读96次

对于html源码的处理,我是写在一个NSString的类别中。

过滤html源码中的图片

- (NSArray *)htmlStringFilterImages
{
    NSMutableArray *resultArray = [NSMutableArray array];
    if (self && 0 < self.length)
    {
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(img|IMG)(.*?)(/>|></img>|>)" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
        NSArray *result = [regex matchesInString:self options:NSMatchingReportCompletion range:NSMakeRange(0, self.length)];
        
        for (NSTextCheckingResult *item in result)
        {
            NSRange range = [item rangeAtIndex:0];
            NSString *imgHtml = [self substringWithRange:range];
            NSArray *tmpArray = nil;
            if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound)
            {
                tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];
            }
            else if ([imgHtml rangeOfString:@"src="].location != NSNotFound)
            {
                tmpArray = [imgHtml componentsSeparatedByString:@"src="];
            }
            
            for (NSString *image in tmpArray)
            {
                if ([image hasPrefix:@"https://"] || [image hasPrefix:@"http://"])
                {
                    NSRange range = [image rangeOfString:@".jpg"];
                    if (range.location == NSNotFound)
                    {
                        range = [image rangeOfString:@".png"];
                    }
                    if (range.location != NSNotFound)
                    {
                        NSString *url = [image substringToIndex:(range.location + range.length)];
                        [resultArray addObject:url];
                    }
                }
            }
        }
    }
    return resultArray;
}

html源码中的图片大小处理(JS)

/// 修改html中图片大小的js代码
- (NSString *)htmlStringJSImageSizeWidth:(float)width
{
    return [NSString stringWithFormat:@"var script = document.createElement('script');script.type = 'text/javascript';script.text = \"function ResizeImages() { var imgs = document.getElementsByTagName('img');for (var i = 0; i < imgs.length; i ++) {var img = imgs[i];img.style.width = %@ ;img.style.height = null;}}\";document.getElementsByTagName('head')[0].appendChild(script);", @(width)];
}

html源码中图片大小自适应屏幕大小(非JS)

- (NSString *)htmlStringImageAutoSize
{
    NSString *content = self;
    if (content && 0 < content.length)
    {
        content = [content stringByReplacingOccurrencesOfString:@"<html>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</html>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"<head>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</head>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</body>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</body>" withString:@""];
        content = [NSString stringWithFormat:@"<html> <head> </head> <body> <script type='text/javascript'> window.onload = function(){ var $img = document.getElementsByTagName('img'); for(var p in  $img){ $img[p].style.width = '100%%'; $img[p].style.height ='auto' } } </script>%@</body> </html>",content];
    }
    return content;
}

修改html源码中的字符字体大小

- (NSString *)htmlStringFontSize:(int)fontsize
{
    NSString *content = self;
    if (content && 0 < content.length)
    {
        content = [content stringByReplacingOccurrencesOfString:@"<html>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</html>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"<head>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</head>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</body>" withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"</body>" withString:@""];
        content = [NSString stringWithFormat:@"<html> <head> <style type=\"text/css\"> body {margin:10;font-size: %@;} </style> </head> <body>%@</body> </html>", (self.htmlContantImage ? @(fontsize) : @(fontsize * 1.5)), content];
    }
    return content;
}

相关文章

网友评论

      本文标题:html代码处理(如图片、字体大小)

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