HTML5 extension of UIWebView

Source: Internet
Author: User
Tags tagname uikit

For many iOS developers, the content of HTML5 is rather unfamiliar.

especially in the UIWebView class. Stringbyevaluatingjavascriptfromstring Method

Many people feel that they have to learn a new language.

And this part of the project is also one of the questions that students often ask.

This article expands the UIWebView class by category (category) and encapsulates some common JavaScript operations into UIWebView class methods.


Latest Source code: HTTPS://GITHUB.COM/DUZIXI/UIWEBVIEW-HTML5 (Continuous maintenance)


header file ( uiwebview+html5.h ):

  uiwebview+html5.h////  Created by Chieh 14-6-30.//  Copyright (c) 2014 lanou3g.com Blue Gull. All rights reserved.//#import <UIKit/UIKit.h> @interface UIWebView (JavaScript)///  Get the number of nodes for a tag-(int) Nodecountoftag: (NSString *) tag;///  get the current page url-(NSString *) getcurrenturl;///  get title-(NSString *) gettitle;///  Get Picture-(Nsarray *) getimgs;///  get current page All links-(Nsarray *) getonclicks;///  change background color-(void) SetBackgroundColor: ( Uicolor *) color;///  Add a click event for all pictures (some images in the page are not valid)-(void) addclickeventonimg;///  change the font color of the specified label-(void) Setfontcolor: (Uicolor *) color Withtag: (NSString *) tagname;///  Change the font size of the specified label-(void) setfontsize: (int) Size Withtag :(NSString *) tagName; @end


implementation file ( UIWEBVIEW+HTML5.M ):

<span style= "FONT-SIZE:14PX;" >////uiwebview+html5.m////Created by Chieh XI on 14-6-30.//Copyright (c) 2014 lanou3g.com Blue Gull.  All rights reserved.//#import "Uiwebview+html5.h" #import "uicolor+change.h" @implementation UIWebView (JavaScript)// Gets the number of nodes of a label-(int) Nodecountoftag: (NSString *) tag{nsstring *jsstring = [NSString stringwithformat:@]    document.getElementsByTagName ('%@ '). Length ", tag];    int len = [[self stringbyevaluatingjavascriptfromstring:jsstring] intvalue]; return Len;} Gets the current page url-(NSString *) getcurrenturl{return [self stringbyevaluatingjavascriptfromstring:@ Document.location.href "];} Get title-(NSString *) gettitle{return [self stringbyevaluatingjavascriptfromstring:@ "document.title"];}        Get all picture links-(Nsarray *) getimgs{Nsmutablearray *arrimgurl = [[Nsmutablearray alloc] init]; for (int i = 0; i < [self nodecountoftag:@ "img"]; i++) {NSString *jsstring = [NSString stringwithformat:@] Docum Ent.getelementsbytagname (' img ') [%D].SRC],I];    [Arrimgurl addobject:[self stringbyevaluatingjavascriptfromstring:jsstring]; } return Arrimgurl;}        Get the current page all click Links-(Nsarray *) getonclicks{Nsmutablearray *arronclicks = [[Nsmutablearray alloc] init]; for (int i = 0; i < [self nodecountoftag:@ "a"]; i++) {NSString *jsstring = [NSString stringwithformat:@] Documen        T.getelementsbytagname (' a ') [%d].getattribute (' onclick ') ", I];        NSString *clickstring = [self stringbyevaluatingjavascriptfromstring:jsstring];        NSLog (@ "%@", clickstring);    [Arronclicks addobject:clickstring]; } return arronclicks;} Change background color-(void) SetBackgroundColor: (Uicolor *) color{NSString * jsstring = [NSString stringwithformat:@ "Document.bod    Y.style.backgroundcolor = '%@ ' ", [color webcolorstring]]; [Self stringbyevaluatingjavascriptfromstring:jsstring];}        Add a click event for all pictures (some images in the page are not valid)-(void) addclickeventonimg{for (int i = 0; i < [self nodecountoftag:@ "img"]; i++) { Take advantage of redirection to get img.src, to differentiate, add ' I to URL 'MG: ' prefix nsstring *jsstring = [NSString stringWithFormat: @ ' document.getelementsbytagname (' img ') [%D].ONCLI        ck = function () {document.location.href = ' img: ' + this.src;} ', I];    [Self stringbyevaluatingjavascriptfromstring:jsstring]; }}///Change the font color of the specified label-(void) Setfontcolor: (Uicolor *) color Withtag: (NSString *) tagname{nsstring *jsstring = [NSString str           Ingwithformat: @ "var nodes = document.getElementsByTagName ('%@ ');    for (Var i=0;i<nodes.length;i++) {Nodes[i].style.color = '%@ ';} ', TagName, [color webcolorstring]]; [Self stringbyevaluatingjavascriptfromstring:jsstring];} Change the font size of the specified label-(void) setfontsize: (int) size Withtag: (NSString *) tagname{nsstring *jsstring = [NSString stringwithfor           Mat: @ "var nodes = document.getElementsByTagName ('%@ ');    for (Var i=0;i<nodes.length;i++) {nodes[i].style.fontsize = '%dpx ';} ", TagName, size]; [Self STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JSSTRing];} @end </span>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.