This library supports Korean string initial matching(초성 매칭) and partial matching(부분 매칭). (这个库支持韩语字符串的 首字母匹配(초성 매칭) 和 部分匹配(부분 매칭)。)
To run the example project, clone the repo, and run pod install
from the Example directory first. (要运行示例项目,请克隆存储库,并首先从 Example 目录运行 pod install
。)
let datas: [String] = ["예그리나", "늘품", "아슬라", "가온누리"]
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
var filteredData: [String] = []
let koreanStringSearchHelper: KoreanStringSearchHelper = KoreanStringSearchHelper(data: [])
@IBAction func initialSwitchCallback(_ sender: UISwitch) {
if sender.isOn {
koreanStringSearchHelper.setInitialMatching(value: true)
} else {
koreanStringSearchHelper.setInitialMatching(value: false)
}
filteredData = koreanStringSearchHelper.getMatchedDatas(text: searchBar.text ?? "")
table.reloadData()
}
@IBAction func partialSwitchCallback(_ sender: UISwitch) {
if sender.isOn {
koreanStringSearchHelper.setPartialMatching(value: true)
} else {
koreanStringSearchHelper.setPartialMatching(value: false)
}
filteredData = koreanStringSearchHelper.getMatchedDatas(text: searchBar.text ?? "")
table.reloadData()
}
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
koreanStringSearchHelper.setDatas(datas: datas)
self.table.dataSource = self
self.table.delegate = self
self.searchBar.delegate = self
self.searchBar.placeholder = "검색어를 입력하세요."
filteredData = datas
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.filteredData = koreanStringSearchHelper.getMatchedDatas(text: searchText)
table.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = false
self.searchBar.text = ""
self.searchBar.resignFirstResponder()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = false
self.searchBar.text = ""
self.searchBar.resignFirstResponder()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.table.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
cell.textLabel?.text = filteredData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count
}
}
Return data set that matches with given text. (返回与给定文本匹配的数据集。)
Chooose whether user initial matching. (选择是否使用首字母匹配。)
Chooose whether user partial matching. (选择是否使用部分匹配。)
Set data set to search. (设置要搜索的数据集。)
Add data set to search. (添加要搜索的数据集。)
Remove data set to search. (移除要搜索的数据集。)
KoreanStringSearchHelper is available through CocoaPods. To install it, simply add the following line to your Podfile (KoreanStringSearchHelper 可以通过 CocoaPods 获取。 要安装它,只需将以下行添加到您的 Podfile 中)
pod 'KoreanStringSearchHelper'
KoreanStringSearchHelper is available under the MIT license. See the LICENSE file for more info. (KoreanStringSearchHelper 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。)
참고자료 http://minsone.github.io/mac/ios/linear-hangul-in-objective-c-swift (参考资料 http://minsone.github.io/mac/ios/linear-hangul-in-objective-c-swift)