导航:首页 > 净水问答 > nsstring过滤html

nsstring过滤html

发布时间:2022-09-02 05:07:05

A. iOS打包Framework静态库,从Framework里加载本地html,怎么加载

方法/步骤 方法一: 通过webview的delegate方法 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; 在上面这个函数中,通过截取NSURLRequest解析js中传递过来的参数,和网址再根据参数来调用已定义好的方法。 但现在我们介绍另外一种方法。 方法二:我们用 javascriptCore.framework 这个库。 首先在建立一个UIWebView,代码如下: #import "webview.h" #import <JavaScriptCore/JavaScriptCore.h> @implementation webview -(id)initWithFrame:(CGRect)frame { self=[super initWithFrame:frame]; if( self ){ self.webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 310, self.bounds.size.width, 300)]; self.webview.backgroundColor=[UIColor lightGrayColor]; NSString *htmlPath=[[NSBundle mainBundle] resourcePath]; htmlPath=[htmlPath :@"html/index.html"]; NSURL *localURL=[[NSURL alloc]initFileURLWithPath:htmlPath]; [self.webview loadRequest:[NSURLRequest requestWithURL:localURL]]; [self addSubview:self.webview]; JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; context[@"log"] = ^() { NSLog(@"+++++++Begin Log+++++++"); NSArray *args = [JSContext currentArguments]; for (JSValue *jsVal in args) { NSLog(@"%@", jsVal); } JSValue *this = [JSContext currentThis]; NSLog(@"this: %@",this); NSLog(@"-------End Log-------"); }; } return self; } @end 在上面代码中,我们先引入了javascriptCore.framework这个库,然后webview那一套就不多说了,注意我加载一个静态网页。然后我用 JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; 获取该UIWebview的javascript执行环境。 在该javascript执行环境中,定义一个js函数,注意关键点来了,这个函数的执行体完全是 objective-c代码写的,也就是下面: context[@"jakilllog"] = ^() { NSLog(@"Begin Log"); NSArray *args = [JSContext currentArguments]; for (JSValue *jsVal in args) { NSLog(@"%@", jsVal); } JSValue *this = [JSContext currentThis]; NSLog(@"-------End Log-------"); }; oc端已经写好了,我们现在进行html部分。 看看UIWebView 中所加载的 html及其js代码是如何写的。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content=""> <meta name="viewport" content="width=device-width; initial-scale=1.0"> <script type="text/javascript" src="index.js"></script> </head> <button id="hallo" onclick="buttonClick()"> 点击button</button> </body> </html> 上面html定义了一个button,然后引用index.js,点击button的响应函数为buttonClick() 。 该函数在index.js中定义,如下 function buttonClick() { jakilllog("hello world"); } 注意,jakilllog("hello world"); 函数名jakilllog才是我们oc端调用的 oc端调用时的代码。 context[@"jakilllog"] = ^() { NSLog(@"Begin Log"); NSArray *args = [JSContext currentArguments]; for (JSValue *jsVal in args) { NSLog(@"%@", jsVal); } JSValue *this = [JSContext currentThis]; NSLog(@"-------End Log-------"); }; 现在的流程是,点击button按钮,响应buttonClick(),去掉用buttonClick()这个方法 function buttonClick() { jakilllog("hello world"); } 然后执行jakilllog("hello world"); 并传参“hello world“ 这个函数。这个函数实现在我们oc端,所以调用方法: context[@"jakilllog"] = ^() { NSLog(@"Begin Log"); NSArray *args = [JSContext currentArguments]; for (JSValue *jsVal in args) { NSLog(@"%@", jsVal); } JSValue *this = [JSContext currentThis]; NSLog(@"-------End Log-------"); };

B. 如何在 NSString 中使用

在ios中 可以使用函数过滤字符串中的特殊符号

首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号

NSCharacterSet *set = [NSCharacterSet :@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];

由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的

然后调用

NSString *trimmedString = [string :set];

trimmedString就是过滤后的字符串

----------------------------------------------------------
http://blog.sina.com.cn/s/blog_5421851501014xif.html

去除 username中的空格,table newline,nextline
代码如下:(三行代码)
NSCharacterSet *whitespace = [NSCharacterSet ];
NSString * username = [mUsernameField stringValue];
username = [username :whitespace];
注释:
:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

Returns a character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
另外可以用 whitespaceCharacterSet 替换 区别newline nextline
whitespaceCharacterSet
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).

NSString *temptext = [messageTextField.text :[NSCharacterSet whitespaceCharacterSet]];
NSString *text = [temptext :[NSCharacterSet ]];
第1行是去除2端的空格
第2行是去除回车

C. 怎么用WebView加载本地html

xml

[html] view plain

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

tools:context=".MainActivity" >

<TextView

android:id="@+id/tv"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

<WebView

android:layout_below="@+id/tv"

android:id="@+id/webview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

</RelativeLayout>


java

[ruby] view plain

package com.example.webview_workflowy;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.webkit.WebView;

import android.widget.Toast;

public class MainActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//加载页面

webView = (WebView) findViewById(R.id.webview);

//允许JavaScript执行

webView.getSettings().setJavaScriptEnabled(true);

//找到Html文件,也可以用网络上的文件

webView.loadUrl("file:///android_asset/index.html");

// 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法

webView.addJavascriptInterface(new Contact(), "contact");

}

private final class Contact {

//JavaScript调用此方法拨打电话

public void call(String phone) {

// startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)));

Toast.makeText(MainActivity.this, phone, Toast.LENGTH_LONG).show();

}

//Html调用此方法传递数据

public void showcontacts() {

String json = "[{"name":"zxx", "amount":"9999999", "phone":"18600012345"}]";

// 调用JS中的方法

webView.loadUrl("javascript:show('" + json + "')");

}

public void toast(String str){

Toast.makeText(MainActivity.this, "aaaaaaaaaaaa --- " + str, Toast.LENGTH_LONG).show();

}

}

}


html

[html] view plain

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

function show(jsondata){

var jsonobjs = eval(jsondata);

var table = document.getElementById("personTable");

for(var y=0; y<jsonobjs.length; y++){

var tr = table.insertRow(table.rows.length);

var td1 = tr.insertCell(0);

var td2 = tr.insertCell(1);

td2.align = "center";

var td3 = tr.insertCell(2);

td3.align = "center";

td1.innerHTML = jsonobjs[y].name;

td2.innerHTML = jsonobjs[y].amount;

td3.innerHTML = "<a href='javascript:contact.call(""+ jsonobjs[y].phone+ "")'>"+ jsonobjs[y].phone+ "</a>";

}

}

</script>

</head>

<body onload="javascript:contact.showcontacts()">

<button id="button" onclick = "javascript:contact.toast('123')">haha</button>

<table border="0" width="100%" id="personTable" cellspacing="0">

<tr>

<td width="30%">姓名</td>

<td width="30%" align="center">存款</td>

<td align="center">电话</td>

</tr>

</table>

</body>

</html>

D. 小程序开发怎么过滤掉json中的html标签

</span></span></p>";

[self flattenHTML:str];

}

- (NSString *)flattenHTML:(NSString *)html {

NSScanner *theScanner;
NSString *text = nil;

theScanner = [NSScanner scannerWithString:html];

while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html :
[NSString stringWithFormat:@"%@>", text]
withString:@""];
} // while //

NSLog(@"-----===%@",html);
return html;
}

E. ios开发中NSArray怎么用NSPredicate来过滤数组内容

//找出一个数组
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
NSError *error;

NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]
//匹配字符串,反回结果, SELF==表示数组中每一个元素
NSString *match = @"imagexyz-999.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

//近似匹配字符串,类似SQL中的语法
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
//不区分大小写匹配
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
//正则匹配
NSString *match = @"imagexyz-//d{3}//.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

F. IOS中如何显示带有html标签的富文本

-(void)viewDidLoad
{
[superviewDidLoad];

NSString*strHTML=@"<p>你好</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这是一个例子,请显示</p><p>外加一个table</p><table><tbody><trclass="firstRow"><tdvalign="top"width="261">aaaa</td><tdvalign="top"width="261">bbbb</td><tdvalign="top"width="261">cccc</td></tr></tbody></table><p><br/></p>";

UIWebView*webView=[[UIWebViewalloc]initWithFrame:self.view.frame];
[self.viewaddSubview:webView];

[webViewloadHTMLString:strHTMLbaseURL:nil];
}

G. ios webview 怎么加载本地html 白名单

你可以按照下面的步骤:


步骤一:首先加载本地的html文件


[objc]viewplainprint?
NSURL*baseURL=[NSURLfileURLWithPath:[[NSBundlemainBundle]bundlePath]];
NSString*path=[[NSBundlemainBundle]pathForResource:@"post.dark"ofType:@"html"];
NSString*html=[:pathencoding:NSUTF8StringEncodingerror:nil];
[_javaWebViewloadHTMLString:htmlbaseURL:baseURL];

步骤二:然后调用webView的dai里方法:


[objc]viewplainprint?
-(void)webViewDidFinishLoad:(UIWebView*)webView

[objc]viewplainprint?
[:JSstringAction];

可以用这个方法向js文件进行传值,调用js方法,给js方法设置参数:

H. IOS打开一个本地html,怎么给他带上一个参数

本地文件不可以url直接加参数,可以在uiwebview 加载好后 用js和oc 交互,传递参数。
参考:

NSURL *urlNosmoke = [NSURL URLWithString:[NSString stringWithFormat:@"nosmokeProgram.html"]];

NSString *strFilePath = [self.viewController.commandDelegate pathForResource:[urlNosmoke path]];

NSURL *urlNonSmoking = [NSURL URLWithString:@"?nonSmokingId=2013" relativeToURL:[NSURL URLWithString:strFilePath]];

NSURLRequest *req = [[NSURLRequest alloc] initWithURL:urlNonSmoking
cachePolicy:
timeoutInterval:20.0f];

[self.viewController.webViewEngine loadRequest:req];

I. ios怎么去掉html标签

-(NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScanner scannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
//找到标签的起始位置
[scanner scanUpToString:@"<" intoString:nil];
//找到标签的结束位置
[scanner scanUpToString:@">" intoString:&text];
//替换字符
html = [html :[NSString stringWithFormat:@"%@>",text] withString:@""];
}
// NSString * regEx = @"<([^>]*)>";
// html = [html :regEx withString:@""];
return html;
}

J. swift怎么读取本地的html文件

把html文件放在项目路径下,然后:var filepath:NSString = NSBundle.mainBundle().pathForResource("test", ofType: "html")

Swift是苹果公司在WWDC2014上发布的全新开发语言。从演示视频及随后在appstore上线的标准文档看来,语法内容混合了OC,JS,Python,语法简单,使用方便,并可与OC混合使用。
全新的编程语言Swift改变了Obejective-C复杂的语法,并保留了Smalltalk的动态特性,简而言之就是敏捷易用。

阅读全文

与nsstring过滤html相关的资料

热点内容
汽车空气滤芯口很脏了有什么影响 浏览:66
山东省污水处理厂运行管理标准 浏览:847
牙缝树脂美学修复 浏览:354
热水器水垢清理 浏览:65
即开即热热水器除垢方法视频 浏览:101
大众新桑塔纳汽油滤芯怎么取下来 浏览:667
蒸馏葡萄白酒如何不列 浏览:890
蒸馏出来的热酒度数 浏览:451
棕榈油的废水 浏览:958
酚氰废水如何产生 浏览:586
污水处理下项目落地流程 浏览:106
7月十日回临汾用隔离 浏览:264
净水器的过滤芯黑色的颗粒是什么 浏览:799
为什么净水器的纯水TDS值偏高 浏览:274
播放器提升不明显 浏览:916
黑壳用过滤水养能活吗 浏览:544
美的饮水机水管有杂味怎么去除 浏览:974
蓝光过滤是不是护眼模式 浏览:675
水壶里面的水垢用什么清洗 浏览:288
养银龙鹦鹉能用珊瑚沙过滤吗 浏览:966