UIWebView 的 HTML5 擴展

http://blog.csdn.net/duzixi/article/details/36047201

 

對於很多iOS開發者來講,HTML5的內容比較陌生。node

尤爲是UIWebView類的 stringByEvaluatingJavaScriptFromString 方法git

讓不少人以爲又得學一種新的語言。github

而這一部分也是項目中學生常問的問題之一。web

本文以Category(類目)的方式擴展了UIWebView類,將一些經常使用的JavaScript操做封裝成UIWebView類方法。字體

 

最新源代碼下載地址:https://github.com/duzixi/UIWebView-HTML5(持續維護)this

 頭文件(UIWebView+HTML5.h)lua

//
//  UIWebView+HTML5.h
//  WebViewJS
//
//  Created by 杜子兮(duzixi) on 14-6-30.
//  Edited  by 杜子兮(duzixi) on 14-7-11. 修改網頁圖片顯示大小
//                                       添加(jQuery)
//  Copyright (c) 2014年 lanou3g.com 藍鷗. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface UIWebView (JavaScript)

#pragma mark -
#pragma mark 獲取網頁中的數據

///  獲取某個標籤的結點個數
- (int)nodeCountOfTag:(NSString *)tag;

///  獲取當前頁面URL
- (NSString *) getCurrentURL;

///  獲取標題
- (NSString *) getTitle;

///  獲取圖片
- (NSArray *) getImgs;

///  獲取當前頁面全部連接
- (NSArray *) getOnClicks;

#pragma mark -
#pragma mark 改變網頁樣式和行爲

///  改變背景顏色
- (void) setBackgroundColor:(UIColor *)color;

///  爲全部圖片添加點擊事件(網頁中有些圖片添加無效)
- (void) addClickEventOnImg;

///  改變全部圖像的寬度
- (void) setImgWidth:(int)size;

///  改變全部圖像的高度
- (void) setImgHeight:(int)size;

///  改變指定標籤的字體顏色
- (void) setFontColor:(UIColor *) color withTag:(NSString *)tagName;

///  改變指定標籤的字體大小
- (void) setFontSize:(int) size withTag:(NSString *)tagName;

@end

 實現文件(UIWebView+HTML5.m):url

//
//  UIWebView+HTML5.m
//
//  Created by 杜子兮(duzixi) on 14-6-30.
//  Edited  by 杜子兮(duzixi) on 14-7-11. 修改網頁圖片顯示大小
//                                       添加(jQuery)
//  Copyright (c) 2014年 lanou3g.com 藍鷗. All rights reserved.
//

#import "UIWebView+HTML5.h"
#import "UIColor+Change.h"

@implementation UIWebView (JavaScript)

#pragma mark -
#pragma mark 獲取網頁中的數據

///  獲取某個標籤的結點個數
- (int)nodeCountOfTag:(NSString *)tag
{
    NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('%@').length", tag];
    int len = [[self stringByEvaluatingJavaScriptFromString:jsString] intValue];
    return len;
}

///  獲取當前頁面URL
- (NSString *)getCurrentURL
{
    return [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];
}

///  獲取標題
- (NSString *)getTitle
{
    return [self stringByEvaluatingJavaScriptFromString:@"document.title"];
}

///  獲取全部圖片連接
- (NSArray *)getImgs
{
    NSMutableArray *arrImgURL = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < [self nodeCountOfTag:@"img"]; i++) {
        NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].src", i];
        [arrImgURL addObject:[self stringByEvaluatingJavaScriptFromString:jsString]];
    }
    return arrImgURL;
}

///  獲取當前頁面全部點擊連接
- (NSArray *)getOnClicks
{
    NSMutableArray *arrOnClicks = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < [self nodeCountOfTag:@"a"]; i++) {
        NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('a')[%d].getAttribute('onclick')", i];
        NSString *clickString = [self stringByEvaluatingJavaScriptFromString:jsString];
        NSLog(@"%@", clickString);
        [arrOnClicks addObject:clickString];
    }
    return arrOnClicks;
}

#pragma mark -
#pragma mark 改變網頁樣式和行爲

///  改變背景顏色
- (void)setBackgroundColor:(UIColor *)color
{
    NSString * jsString = [NSString stringWithFormat:@"document.body.style.backgroundColor = '%@'",[color webColorString]];
    [self stringByEvaluatingJavaScriptFromString:jsString];
}

///  爲全部圖片添加點擊事件(網頁中有些圖片添加無效,須要協議方法配合截取)
- (void)addClickEventOnImg
{
    for (int i = 0; i < [self nodeCountOfTag:@"img"]; i++) {
        //利用重定向獲取img.src,爲區分,給url添加'img:'前綴
        NSString *jsString = [NSString stringWithFormat:
            @"document.getElementsByTagName('img')[%d].onclick = \
              function() { document.location.href = 'img' + this.src; }",i];
        [self stringByEvaluatingJavaScriptFromString:jsString];
    }
}

///  改變全部圖像的寬度
- (void) setImgWidth:(int)size
{
    for (int i = 0; i < [self nodeCountOfTag:@"img"]; i++) {
        NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].width = '%d'", i, size];
        [self stringByEvaluatingJavaScriptFromString:jsString];
    }
}

///  改變全部圖像的高度
- (void) setImgHeight:(int)size
{
    for (int i = 0; i < [self nodeCountOfTag:@"img"]; i++) {
        NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].height = '%d'", i, size];
        [self stringByEvaluatingJavaScriptFromString:jsString];
    }
}

///  改變指定標籤的字體顏色
- (void)setFontColor:(UIColor *)color withTag:(NSString *)tagName
{
    NSString *jsString = [NSString stringWithFormat:
        @"var nodes = document.getElementsByTagName('%@'); \
          for(var i=0;i<nodes.length;i++){\
              nodes[i].style.color = '%@';}", tagName, [color webColorString]];
    [self stringByEvaluatingJavaScriptFromString:jsString];
}

///  改變指定標籤的字體大小
- (void)setFontSize:(int)size withTag:(NSString *)tagName
{
    NSString *jsString = [NSString stringWithFormat:
        @"var nodes = document.getElementsByTagName('%@'); \
          for(var i=0;i<nodes.length;i++){\
              nodes[i].style.fontSize = '%dpx';}", tagName, size];
    [self stringByEvaluatingJavaScriptFromString:jsString];
}
@end

 原文出處:http://blog.csdn.net/duzixi/article/details/36047201spa

相關文章
相關標籤/搜索