preface

In fact, this library has been created early and completed the first version, support full spelling + initials + Chinese and English + polyphonic word search, the function is still powerful. I haven’t been paying attention to it since. This time, with BUG fixes and a swift release, it paid off a technical debt.

Project address: Click me

Function is introduced

The library is currently available in ObjectiveC and Swift versions. All support full spelling + first letter + Chinese and English + polyphonic search.

See the picture below for the specific effect:

The principle is introduced

  1. Load pinyin resource file, initial configuration
  2. Taking keyword matching as an example, precise matching takes precedence
  3. Accurate matching is not found, so consider turning the search text into pinyin initials and keyword to search whether the string of pinyin initials is matched
  4. If the first letter of pinyin does not match, then consider whether full spelling can match

To put it bluntly, the realization principle is relatively simple. It is mainly to list the permutations and combinations of various characters in pinyin, and then to match one by one.

use

OC edition

NSArray *ranges;
BOOL match = [self.textLabel.text canMatchWithKeyword:searchText allRanges:&ranges];
if (match) {
    ...
}
Copy the code

Swift version

var ranges: [Range<String.Index>] = []
text.canMatch(with: searchText, all: &ranges)
if ranges.count > 0 {
    .
}
Copy the code

How to integrate

The ObjectiveC project is introduced as a POD component library. Perform:

pod 'IASearchKit'.'~ > 1.2.0'
Copy the code

For Swift projects, pod component library is introduced. Perform:

pod 'KeySearch'.'~ > 0.1.5'
Copy the code

Source address: point I jump