Recently made a note-type app, mainly for drilling RN development technology. which has a search function.
A problem was identified during the implementation of this feature:
For example, I enter China, each callback to the text is in the g-> gu so, not convenient for the search key. For iOS native development, this scenario can be resolved by judging the input type and combining the use of the following APIs:
-(void) Searchtextfielddidchange: (Uitextfield *) TextField
{
NSString *lang = [[Uitextinputmode currentinputmode] primarylanguage]; Keyboard Input Mode
if (![ Chineseinclude IsIncludeChineseInString:self.searchField.text] && [self.searchField.text length] < 2)
{
Less than 2 English characters, do not start the search; When text is emptied, only the default receptionist is displayed.
if ([self.searchField.text length] = = 0)
{
Self.matchedreceivers = [self selectedtypedefaultreceivers];
[Self.popuplist Reloaddata];
}
Return
}
if ([lang isequaltostring:@ "Zh-hans"])
{
Simplified Chinese input, including simplified pinyin, fitness wubi, simplified handwriting
Uitextrange *selectedrange = [TextField markedtextrange];
uitextposition* SelectionStart = Selectedrange.start;
uitextposition* selectionend = selectedrange.end;
Nsinteger length = [TextField offsetfromposition:selectionstart toposition:selectionend];
if (length!= 0)
{
Return
}
Else
{
Ddloginfo (@ "searchtextfielddidchangeinreal,%@", self.searchField.text);
[NSObject cancelpreviousperformrequestswithtarget:self selector: @selector (dothesearching) Object:nil];
[Self performselector: @selector (dothesearching) Withobject:nil afterdelay:0.5];
}
}
Else
{
[NSObject cancelpreviousperformrequestswithtarget:self selector: @selector (dothesearching) Object:nil];
[Self performselector: @selector (dothesearching) Withobject:nil afterdelay:0.5];
}
}
The main is to filter out the contents of the Markedtextrange, if in this state does not trigger the callback. For example, I enter the "medium" will trigger the search, but the input word half of the time will not take "G" to do the search match. But such an effect is currently difficult to solve from RN here.
Finally put a picture for everyone to understand this scene.