gpt4 book ai didi

swift - 如何使用Swift在WebView中查找和突出显示文本

转载 作者:行者123 更新时间:2023-11-28 15:33:21 25 4
gpt4 key购买 nike

我正在开发我的第一个应用程序,它包括从一个网站上的大量文本中搜索特定的文本字符串。我希望能够搜索特定的字符串并突出显示所有实例,并且随着用户键入更多,它将自动更新突出显示的实例。类似于任何浏览器中的查找功能。
我在这里找到了this问题,他们在哪里引用了UIWebViewSearch.js来实现这一点,但我不确定如何将该文件添加到我的项目中,或者如何使用它。
到目前为止,我已经在我的应用程序中添加了一个搜索栏,但没有对它做任何操作。希望与搜索栏一起使用此功能来搜索文本。
这是我的ViewController代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)


// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
} // end if


func searchBarSearchButtonClicked() {
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(str)')"

self.newsWebView .stringByEvaluatingJavaScript(from: startSearch)
}


} // end of viewDidLoad


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // end of didReceiveMemoryWarning


} // end of class ViewController

最佳答案

正如@jon saw所说:欢迎来到StackOverflow:)
我只回答如何将JavaScript文件添加到您的项目中…这应该会使事情正确开始:)
添加文件
您可以通过多种方式将文件添加到Xcode,但这里有一种方法。
在Xcode的项目中,在左侧的项目导航器中导航到要添加文件的位置。
Select location
右键单击并选择“新建文件…”
New file
选择“空”并单击“下一步”
enter image description here
将文件命名为UIWebViewSearch.js并单击“创建”
现在您的项目中有一个名为UIWebViewSearch.js的空文件,可以在其中粘贴内容。
使用文件
现在你已经有了你的项目文件,所以现在你只需要参考它。为此,您可以使用Bundle为您加载项目中的资源。
如果您查看所引用的the answer,您可以在- (NSInteger)highlightAllOccurencesOfString:(NSString*)str中看到这些ObjC代码行

NSString *path = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

翻译成这样的Swift:
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
}

这将为您提供一个包含所有JS代码的 String,可以添加到WebView中。
希望这能让你有所收获。
更新(在您向本文添加评论之后)
好吧,你说:
我添加了ViewController代码,以便更好地与所在位置进行通信。对于如何实现你所指的他的代码的第二部分有些困惑。也不知道如何从搜索栏调用搜索。“searchBarSearchButtonClicked()方法中是否需要调用方法?”?刚刚在苹果开发者文档中找到了这个方法,但不知道它是否正确。
让我们把它分解成碎片,我将从您的 ViewController开始,因为您的 viewDidLoad中存在一些问题:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
} // end if


func searchBarSearchButtonClicked() {
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(str)')"

self.newsWebView .stringByEvaluatingJavaScript(from: startSearch)
}
}

您已经在 searchBarSearchButtonClicked中添加了 viewDidLoad,但它本身应该声明为一个函数(稍后我们将继续讨论)。
此外,正如我在下面的评论中所写:
…一个在加载视图时运行的部分,它从包中加载JavaScript。
所以让我们修复您的 viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
//Use your jsString on the web view here.
newsWebView.stringByEvaluatingJavaScript(from: jsString)
}
}

如您所见, searchBarSearchButtonClicked函数已被删除,我们现在在 jsString上使用out newsWebView
好的,下一部分:
也不知道如何从搜索栏调用搜索。“searchBarSearchButtonClicked()方法中是否需要调用方法?”?刚刚在苹果开发者文档中找到了这个方法,但不知道它是否正确。
你有你在Apple开发者文档中找到的 searchBarSearchButtonClicked()方法,我猜你的意思是 this one
正如您在文档(顶部栏)中看到的,这个函数来自 UISearchBarDelete类。
Delegation是许多apple框架中使用的设计模式。它允许您编写一些通用组件,并让“其他人”(委托人)决定如何处理组件中的信息(我知道错误的解释)。
例如,想想你的 UISearchBar。如果您要实现一个组件,其他开发人员可以将其用作搜索栏,那么当用户搜索某些内容时,您将如何处理?我在想,您的“客户”(使用组件的开发人员)应该如何处理这个问题?这样做的一种方式可能是,开发人员应该子类化 UISearchBar并重写“用户执行的搜索方法”。您还可以使用 NSNotifications并让开发人员注册这些通知。这两种方法都不漂亮也不聪明。
输入…代表。
UISearchBar有一个 UISearchBarDelegate并且 UISearchBarDelegate只是一个协议,如您所见。现在 UISearchBar可以向其代表询问某些事情,或向代表通知重要事件。例如,当用户点击“搜索”按钮时, UISearchBar可以调用 delegate.searchBarSearchButtonClicked(self),这意味着“嘿,代表,用户点击了我身上的搜索按钮……只是觉得你应该知道”。然后,代表可以在其认为合适的时候作出回应。
这意味着任何类都可以符合 UISearchBarDelegate并根据其当前情况处理各种任务。
好吧,长话短说,希望你能理解它的要点,我认为你应该多读一点关于委托的内容,因为它是Apples框架中使用的一种模式:)
因此,在您的情况下,您有一个 UISearchBar,您应该给它一个这样的委托:
searchBar.delegate = self

您需要告诉编译器您打算实现 UISearchBarDelegate协议。Swift惯例是在您的 extension之后在 ViewController中执行此操作,只是为了将事情分开:
extension ViewController: UISearchBarDelegate {

}

然后,您还必须实现您感兴趣的 UISearchBarDelegate方法(注意,有些协议有必需的方法,这意味着您必须实现它们,但我认为 UISearchBar没有(否则您将在第一次运行时发现应用程序崩溃:))。
所以在你的例子中,你提到了 searchBarSearchButtonPressed。如您所见,它需要一个 UISearchBar作为参数。所以你的函数看起来是这样的:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let currentSearchText = searchBar.text else {
return
}
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(currentSearchText)')"

newsWebView.stringByEvaluatingJavaScript(from: startSearch)
}

所以。。。从searchBar获取当前文本,将其作为参数添加到 startSearch字符串中,并将 startSearch字符串传递到 newsWebView字符串中。
整个扩展都是这样结束的。
.... last part of your ViewController class
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // end of didReceiveMemoryWarning
} // end of class ViewController

extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let currentSearchText = searchBar.text else {
return
}
let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(currentSearchText)')"

newsWebView.stringByEvaluatingJavaScript(from: startSearch)
}
}

而您的 viewDidLoad的结尾是这样的(您必须将自己添加为搜索栏的代理)
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self //delegate set up
// Do any additional setup after loading the view, typically from a nib.

// Place custom HTML onto the screen
let myURL = Bundle.main.url(forResource: "myHTML", withExtension: "html")
let requestObj = URLRequest(url: myURL!)
webView.loadRequest(requestObj)
// Code that provides a string containing
// JS code, ready to add to a webview.
if let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js"),
let jsString = try? String(contentsOfFile: path, encoding: .utf8) {
print(jsString)
//Use your jsString on the web view here.
newsWebView.stringByEvaluatingJavaScript(from: jsString)
}
}

哇…写了很多东西…希望对你有帮助。

关于swift - 如何使用Swift在WebView中查找和突出显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44535699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com