http://blog.csdn.net/duzixi/article/details/36047201
对于不少iOS开发者来说,HTML5的内容比较陌生。
尤其是UIWebView类的 stringByEvaluatingJavaScriptFromString 方法
让很多人觉得又得学一种新的语言。
而这一部分也是项目中学生常问的问题之一。
本文以Category(类目)的方式扩展了UIWebView类,将一些常用的JavaScript操作封装成UIWebView类方法。
最新源代码下载地址:https://github.com/duzixi/UIWebView-HTML5(持续维护)
头文件(UIWebView+HTML5.h)
//
// 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):
//
// 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