【发布时间】:2011-05-26 10:29:34
【问题描述】:
我的 webview 中有一些从 html 页面加载的文本。现在,如果我想从 Webview 中选择一个特定的单词,那么应该突出显示选定的单词。
我们可以在 weebview 中实现这种类型的功能吗?
【问题讨论】:
-
你能接受你之前的回答吗?
-
您的问题解决了吗?
标签: objective-c xcode string uiwebview
我的 webview 中有一些从 html 页面加载的文本。现在,如果我想从 Webview 中选择一个特定的单词,那么应该突出显示选定的单词。
我们可以在 weebview 中实现这种类型的功能吗?
【问题讨论】:
标签: objective-c xcode string uiwebview
@interface UIWebView (SearchWebView)
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
@end
@implementation UIWebView (SearchWebView)
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:@"MyApp_HighlightAllOccurencesOfString('%@')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [self stringByEvaluatingJavaScriptFromString:@"MyApp_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:@"MyApp_RemoveAllHighlights()"];
}
@end
【讨论】: