導航:首頁 > 凈水問答 > 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相關的資料

熱點內容
污水處理cod加石灰作用 瀏覽:727
飲水機是怎麼快速加熱的 瀏覽:980
洗胃器一小時凈水多少噸 瀏覽:308
水裡重金屬過濾 瀏覽:418
純凈水處理除垢 瀏覽:908
機油濾芯用什麼機器好 瀏覽:736
污水站運營方案運營特色服務 瀏覽:228
反滲透設備3d動畫 瀏覽:463
低溫蒸餾法最低溫度 瀏覽:680
碧然德濾水壺濾芯有什麼區別 瀏覽:134
蒸餾高一視頻 瀏覽:102
汽車空氣濾芯口很臟了有什麼影響 瀏覽:66
山東省污水處理廠運行管理標准 瀏覽:847
牙縫樹脂美學修復 瀏覽:354
熱水器水垢清理 瀏覽:65
即開即熱熱水器除垢方法視頻 瀏覽:101
大眾新桑塔納汽油濾芯怎麼取下來 瀏覽:667
蒸餾葡萄白酒如何不列 瀏覽:890
蒸餾出來的熱酒度數 瀏覽:451
棕櫚油的廢水 瀏覽:958