KoreanStringSearchHelper

Version License Platform SwiftPM

Summary

This library supports Korean string initial matching(초성 매칭) and partial matching(부분 매칭).

Demo App

To run the example project, clone the repo, and run pod install from the Example directory first.

merge_from_ofoct-2

Example

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
    }
}

Documentation

getMatchedDatas(text: String) -> [String]

Return data set that matches with given text.

setInitialMatching(value: Bool) -> void

Chooose whether user initial matching.

setPartialMatching(value: Bool) -> void

Chooose whether user partial matching.

setDatas(datas: [Stinrg]) -> void

Set data set to search.

addData(data: Stinrg) -> void

Add data set to search.

removeData(data: Stinrg) -> void

Remove data set to search.

Installation

KoreanStringSearchHelper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'KoreanStringSearchHelper'

License

KoreanStringSearchHelper is available under the MIT license. See the LICENSE file for more info.

참고자료 http://minsone.github.io/mac/ios/linear-hangul-in-objective-c-swift

GitHub

View Github