I made a page which contained a UITextFiled and a view that presented search result and I want to change the triggering conditions of the search. The original triggering conditions was changing the input text of textField and I want to make some change. I hope the search is triggered only when user touch the keyboard autocorrection. Could someone give me some advance, thank you!
Try implementing UITextChecker like this. The last two lines are the print statement for two types. One is for guesses words when misspelled occurred and the other is for dictionary words. The arrPredectiveText array contains words sequentially like dictionary. You can get suggestions from this.
Call the suggestionsForCustomKeyboard method with some string that you are typing.
-(void)suggestionsForCustomKeyboard:(NSString *)word{
if ([currentString length] >= 1) {
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSRange misspelledRange = [textChecker
rangeOfMisspelledWordInString:currentString
range:NSMakeRange(0, [currentString length])
startingAt:0
wrap:NO
language:#"en_US"];
if (misspelledRange.location != NSNotFound) {
guesses = [textChecker guessesForWordRange:misspelledRange
inString:currentString
language:#"en_US"];
NSLog(#"First guess: %#", [guesses firstObject]);
} else {
NSLog(#"Textchecker Not found");
autocorrectedText = #"";
}
if (arrPredectiveText.count >= 2){
suggestionOne = [arrPredectiveText objectAtIndex:0];
suggestionTwo = [arrPredectiveText objectAtIndex:1];
}
else if (arrPredectiveText.count == 1 && guesses.count >= 1) {
suggestionOne = [arrPredectiveText firstObject];
suggestionTwo = [guesses firstObject];
}else if (arrPredectiveText.count == 0 && guesses.count > 0){
suggestionOne = [guesses firstObject];
if (guesses.count > 1) {
suggestionTwo = [guesses objectAtIndex:1];
}
}
NSLog(#"Textchecker all guess: %#", guesses);
NSLog(#"Prediction: %#",arrPredectiveText);
}
Related
I am making an only number input app (still) in which users press a button, I store a value into a string to display in a label.
Works great for the most part, except I cannot figure out how to prevent users from entering more than one decimal in a single string. .
I looked at this Stack overflow question, but trying to amend the code for my own just resulted in a whole bunch of errors. Does anyone have any advice?
- (void)numberBtn:(UIButton *)sender {
if (self.sales.text.length < 10) {
if(self.sales.text.length != 0){
NSString *lastChar = [self.sales.text substringFromIndex:[self.sales.text length] - 1];
if([lastChar isEqualToString:#"."] && [sender.titleLabel.text isEqualToString:#"."] && [sender.titleLabel.text stringByAppendingString:#"."]){
return;
}
if ([lastChar isEqualToString:#""] && [sender.titleLabel.text isEqualToString:#""]){
self.numbers = #"0.";
}
if ([self.sales.text rangeOfString:#"."].length > 0) {
NSArray *array = [self.sales.text componentsSeparatedByString:#"."];
if (array.count == 2) {
NSString *decimal = array.lastObject;
if (decimal.length > 2) {
return;
}
}
}
}
self.numbers = [NSString stringWithFormat:#"%#%#",self.numbers,sender.titleLabel.text];
self.sales.text = self.numbers;
}
}
Two steps...
Check to see if the button is a decimal .
if yes, see if the current label text already contains a .
If it does, return. If not, continue processing your button input:
- (void)numberBtn:(UIButton *)sender {
NSString *btnTitle = sender.currentTitle;
NSString *curText = self.sales.text;
if ([btnTitle isEqualToString:#"."]) {
if ([curText containsString:#"."]) {
NSLog(#"%# : Already has a decimal point!", curText);
return;
}
}
// whatever else you want to do with the input...
}
I am working on a custom contact,currently i'm using this code to search custom objects
filteredTableData = [[NSMutableArray alloc] init];
for (int i=0; i<contactSectionTitles.count; i++)
{
NSString *sectionTitle = [contactSectionTitles objectAtIndex:i];
NSArray *sectionmContacts = [mContacts objectForKey:sectionTitle];
for (CS_ContactDTO *theDto in sectionmContacts) {
NSRange nameRange = [theDto.mFirstName rangeOfString:text options:NSForcedOrderingSearch];
NSRange descriptionRange = [[theDto.mFirstName description] rangeOfString:text options:NSForcedOrderingSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
if (![filteredTableData containsObject:theDto])//Search DTO
{
[filteredTableData addObject:theDto];
}
}
}
}
Note
"theDTO" is my custom class
It has values FirstName,LastName,PhoneNumber.
The code gives me result like,
For "A" -> "Arun","India","Kumar"
My requirement is like to search
"A" should provide only "Arun"(starting letter matching),not the other results.
Simply change your check to see if the range location is 0:
if (nameRange.location == 0 || descriptionRange.location == 0) {
This will be true if text is at the start of the name or description.
BTW - why do you check both mFirstName and mFirstName description? What's the difference?
If you want to check if NSString starts with a particular search term, you can use
if ([myString hasPrefix:#"searchTerm"])
NSString * currentWord;
currentWord = Text.text;
UITextChecker* checker = [[UITextChecker alloc] init];
NSString* preferredLanguage = [[UITextChecker availableLanguages] objectAtIndex:0];
NSRange range;
range = [checker rangeOfMisspelledWordInString:currentWord
range:NSMakeRange(0, [currentWord length])
startingAt:0
wrap:NO
language:preferredLanguage];
if (range.location == NSNotFound)
{
NSLog(#"Word found");
}
else
{
NSLog(#"Word not found");
}
//Here i used UITextChecker Function ,even for the wrong word, the above function shows correct word statement
eg: wrong word like abcd,
bcde,
cdef,
CAPs, kindly help me what is the reason behind this. Is there any other option to solve this?
Thanks in advance
UITextChecker *Checker = [[UITextChecker alloc] init];
NSRange range = NSMakeRange(0, inputWord.length);
NSRange misspelledRange = [Checker rangeOfMisspelledWordInString:[Sentence lowercaseString] range:range startingAt:0 wrap:NO language:#"en_IN"];
bool isValidWord = misspelledRange.location == NSNotFound;
//NSLog(#"----%i", misspelledRange.location);
if (isValidWord)
{
isValidWord = [self checkIfWordExistsInSuggestedSpellings:Sentence];
NSLog(#"++++%d", isValidWord);
}
return isValidWord;
}
else
{
NSLog(#"Invalid word");
return false;
}
NSNotFound means that whatever you asked for wasn't found. == is equality operator. Your code prints "Word found" when range.location is equal to NSNotFound.
I've set up a simple if statement that says if the input length is equal to 3 make the next character a "-".
It works well, but I'd like the "-" to automatically be put there after I press my button for the 3rd time. So I press, "1", "2", then when I press the "3" it automatically puts a "-" directly afterwards. Currently the "-" only gets placed when I hit the button for the 4th time?
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
int length = [val length];
} else {
NSString *tagValue = [NSString stringWithFormat:#"%d", [sender tag]];
phoneNumberLabel.text = [val stringByAppendingString: tagValue];
if (length == 3) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
if (length == 7) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
}
}
Any help would be appreciated, greatly! Thanks!
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
NSString *newValue = #"";
NSString *dash = #"";
int length = [val length];
if ( ((length == 3) || (length == 7)) ) {
dash = #"-";
}
newValue = [NSString stringWithFormat:#"%#%#%d", val, dash, [sender tag]];
phoneNumberLabel.text = newValue;
}
Add the following code in buttonClicked method
if ([myLabel.text length] == 3)
{
myLabel.text = [val stringByAppendingString:#"-"];
}
Just add that code in the same method after you update the label with the integer.
Try this method,hope it will help you.
Keep UILabel object blank in xib.
- (IBAction)pressAction:(UIButton*)sender
{
if (lbl.text.length<=2) {
lbl.text=[lbl.text stringByAppendingFormat:#"%i",sender.tag];
}
if (lbl.text.length>=3){
lbl.text=[lbl.text stringByAppendingString:#"-"];
}
}
You can send the button the events. like [button sendActionsForControlEvents:UIControlEventTouchUpInside]
Or even perform selector after delay may do the trick for you.
both the cases you need not press a button
For the addition of "-" just check how many characters have been entered and when the third character has been entered add the "-".
I am searching my products like the below. It works fine however when searching between 2 words the results disappear then reappear once the user has entered the first character of the next word. i.e if I search "td pro" or "pro td" the results are there. If I search like this td(i have a result) td(space) I have NO result td p(I have a result) just to add I need to separate the words by space in componentsSeperatedByString because the user may not search for a product in any particular order. Unless there is a better method?
Here is my code:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (XMLStringFile *new in rssOutputData_MutableArray)
{
//Product scope
if ([scope isEqualToString:#"Product"])
{
// Split into search text into separate "words"
NSArray * searchComponents = [searchText componentsSeparatedByString: #" "];
;
BOOL foundSearchText = YES;
// Check each word to see if it was found
for (NSString * searchComponent in searchComponents) {
NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
}
// If all search words found, add to the array
if (foundSearchText)
{
[self.filteredListContent addObject: new];
}
}
}
}
Many thanks
I fixed it with the help of another stack question that someone helped me with.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (XMLStringFile *new in rssOutputData_MutableArray)
{
//Product scope
if ([scope isEqualToString:#"Product"])
{
// Split into search text into separate "words"
NSArray * searchComponents = [searchText componentsSeparatedByString: #" "];
//Before searching trim off the whitespace
searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
BOOL foundSearchText = YES;
// Check each word to see if it was found
for (NSString * searchComponent in searchComponents) {
NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
}
// If all search words found, add to the array
if (foundSearchText)
{
[self.filteredListContent addObject: new];
}
}
}
}
The winning line is:
//Before searching trim off the whitespace
searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];