【问题标题】:How to copy the text from the UIwebview如何从 UIwebview 复制文本
【发布时间】:2011-05-26 10:29:34
【问题描述】:

我的 webview 中有一些从 html 页面加载的文本。现在,如果我想从 Webview 中选择一个特定的单词,那么应该突出显示选定的单词。

我们可以在 weebview 中实现这种类型的功能吗?

【问题讨论】:

  • 你能接受你之前的回答吗?
  • 您的问题解决了吗?

标签: objective-c xcode string uiwebview


【解决方案1】:

Search and highlight text in UIWebView

SearchWebView.h:

@interface UIWebView (SearchWebView)

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;

@end

SearchWebView.m:

@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

【讨论】:

  • 我必须在javascript文件中声明函数吗?
  • 谢谢你能不能显示“SearchWebView.js 文件我们应该写什么内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 2020-09-22
相关资源
最近更新 更多