html程式碼處理(如圖片、字型大小)

weixin_34107955發表於2018-01-30

對於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;
}

相關文章