在我的应用中,我有一个带有详细视图的表格视图。详细信息视图从HERE加载博客帖子,详细信息视图将显示该帖子的文本。我几乎让它工作但我仍然需要弄清楚如何修复文本(下面的例子)。以下是我在详细视图中从网站上提取文本的代码:
-(void)viewDidLoad {
//self.url is from the table view where the link to the website is
NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(LoadText) userInfo:nil repeats:YES];
}
-(void)webViewDidLoadText {
NSString *textForBlog = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('paragraph')[0].innerHTML;"];
self.textView.text = textForBlog;
}
答案 0 :(得分:1)
以您的代码为例,这就是您实现它的方式:
-(void)webViewDidLoadText {
NSString *textForBlog = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('paragraph')[0].innerHTML;"];
// Add this step for stripping the HTML from the text you received
NSString *strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];
self.textView.text = strippedTextForBlog;
}
// Taken from: http://stackoverflow.com/questions/6170531/strip-out-html-tags-etc-from-nsstring/6171866#6171866
- (NSString *)stringByStrippingHTMLFromString:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:@">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}