1

これは私のコードです:

NSURL *url=[NSURL URLWithString:@"http://www.engadget.com"];
NSString *webPage=[[NSString alloc]initWithContentsOfURL:url
                          encoding:NSUTF8StringEncoding error:nil];

webPage 文字列で、リンクの html ページを取得しました。その文字列には、多くのタグとテキストがあります。タグなしでテキストの本文のみを取得したい。

そのテキストを UITextView に表示したいと思います。どうやってやるの?

4

3 に答える 3

1

これが最良の答えであり、まさにあなたが探しているものです:

webView デリゲート メソッドに次のスクリプトを記述します。( UIWebviewdidfinishLoading)

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
于 2009-10-13T09:05:56.393 に答える
0

より良い解決策:

- (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 stringByReplacingOccurrencesOfString:
                           [ NSString stringWithFormat:@"%@>", text]
                     withString:@" "];

    } // while //

    return html;

}

参考: http: //rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c

于 2010-06-25T16:12:16.130 に答える
0

私が試したところ、これが最もうまくいきました。NSSCanner はこれに対するよりスマートなソリューションではありませんが、html/xml が整形式であれば問題ありません。

于 2010-03-31T10:33:27.527 に答える