Reputation:
This is my code:
NSURL *url=[NSURL URLWithString:@"http://www.engadget.com"];
NSString *webPage=[[NSString alloc]initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
In the webPage string I got an html page of the link. In that string there is lot of tags and text. I want to take only body of the text without any tags.
I want to display that text into my UITextView. How can I do that?
Upvotes: 1
Views: 4075
Reputation: 16317
This is the best answer and is exactly what you are looking for:
Write the following script in the webView delegate method. (UIWebviewdidfinishLoading
)
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
Upvotes: 1
Reputation: 652
Better Solution:
- (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;
}
Reference: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c
Upvotes: 0