Я работаю над своим первым приложением, и оно включает в себя поиск определенной строки текста из большого количества текста на веб -сайте. Я хочу иметь возможность искать определенную строку и выделять все экземпляры, и, поскольку пользователь больше набирает больше, она автоматически обновляет выделенные события. Аналогично функции Find в любом браузере. < /p>
Я до сих пор добавил строку поиска в свое приложение, но ничего не сделал с этим. Хочу использовать эту функцию вместе с панелью поиска для поиска текста.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
Подробнее здесь: https://stackoverflow.com/questions/445 ... sing-swift