Uitextfield Autocomplete

swift how to suggest user email in uitextfield as autocomplete

Set textContentType to emailAddress

let textField = UITextField()
textField.textContentType = .emailAddress

iOS UITextField textfield empty after selecting autofill address from contacts

Instead of using:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

}

switch to:

func textFieldDidChange(_ textField: UITextField) {

}

AutoComplete for UITextfield in iOS

Take two Global Array

NSMutableArray *muary_Interest_Main;
NSMutableArray *muary_Interest_Sub;

IN viewDidLoad Method

muary_Interest_Main = [[NSMutableArray alloc]initWithObjects:@"Cricket",@"Dancing",@"Painting",@"Swiming",@"guitar",@"movie",@"boxing",@"drum",@"hockey",@"chessing",@"gamming", 
@"hunting",@"killing",@"shoping",@"jamm"@"zooming", nil];
muary_Interest_Sub = [[NSMutableArray alloc]init];

tbl_Search = [[UITableView alloc] initWithFrame:
CGRectMake(4, 200, 320, 120) style:UITableViewStylePlain];
tbl_Search.delegate = self;
tbl_Search.dataSource = self;
tbl_Search.scrollEnabled = YES;

[self.tbl_Search registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"];
[self.view addSubview:self.tbl_Search];

[tbl_Search setHidden:TRUE];

Now write a below code in textfield delegates.

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
NSLog(@"%d",textField.tag);
int_TextFieldTag = textField.tag;

[self searchText:textField replacementString:@"Begin"];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
tbl_Search.hidden = TRUE;
return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
tbl_Search.hidden = TRUE;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self searchText:textField replacementString:string];
return YES;
}

Write a method for search text

-(void) searchText:(UITextField *)textField replacementString:(NSString *)string
{

if(int_TextFieldTag == 1)
{
tbl_Search.frame = CGRectMake(4, 200, 320, 120);

}
else if(int_TextFieldTag == 2)
{
tbl_Search.frame = CGRectMake(4, 248, 320, 120);
}
else if(int_TextFieldTag == 3)
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}
else if(int_TextFieldTag == 4)
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}
else
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}

NSString *str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
if([string isEqualToString:@"Begin"])
str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
else if([string isEqualToString:@""])
str_Search_String = [str_Search_String substringToIndex:[str_Search_String length] - 1];
else
str_Search_String=[str_Search_String stringByAppendingString:string];

muary_Interest_Sub=[[NSMutableArray alloc] init];
if(str_Search_String.length>0)
{
NSInteger counter = 0;
for(NSString *name in muary_Interest_Main)
{
NSRange r = [name rangeOfString:str_Search_String options:NSCaseInsensitiveSearch];
if(r.length>0)
{
[muary_Interest_Sub addObject:name];
}

counter++;

}

if (muary_Interest_Sub.count > 0)
{
NSLog(@"%@",muary_Interest_Sub);
tbl_Search.hidden = FALSE;
[self.tbl_Search reloadData];
}
else
{
tbl_Search.hidden = TRUE;
}

}
else
{
[tbl_Search setHidden:TRUE];

}

}

Tableview Delegates methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [muary_Interest_Sub count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
;
}
cell.textLabel.text = [muary_Interest_Sub objectAtIndex:indexPath.row];
return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
if(int_TextFieldTag == 1)
{
txt1.text=[muary_Interest_Sub objectAtIndex:indexPath.row];

}
else if(int_TextFieldTag == 2)
{
txt2.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else if(int_TextFieldTag == 3)
{
txt3.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else if(int_TextFieldTag == 4)
{
txt4.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else
{
txt5.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}

}

This also works on backspace of textfield.
Try this. Hope this will suit your requirements.

Autofill for UITextField is broken

So, the issue in general was that apple's autofill was having a difficult time determining if I was creating an account. From their documentation here:

By default, the system selects the a keyboard based on the input
view’s textContentType property; however, you can mix the input view’s
text content type and keyboard type to explicitly define the desired
keyboard. For example, if your site uses email addresses as user
names, set the input view’s textContentType property to .username, and
set the keyboardType property to .UIKeyboardType.emailAddress.

So, for the email address I set the content type to username and the keyboard type to email, and all is fixed.

Shortcuts or autofill for contact info in UITextFields

You can turn on or off it by set autocorrectionType of UITextField. Default value of autocorrectionType is UITextAutocorrectionTypeDefault . It means you don't need to do anything, by default it will be showed.

UITextAutocorrectionTypeYes; // Turn on

UITextAutocorrectionTypeDefault; // Turn on

UITextAutocorrectionTypeNo; // Turn off

But beside of set autocorrectionType value, you need to make sure Predictive in Keyboards Setting of device is turned on. If Predictive is turned off, suggestion won't be displayed even you changed autocorrectionType to UITextAutocorrectionTypeYes.

UPDATE

You can change textContentType to choose with type of suggestion will be shown. Suggestions are from the Contacts. For example, you want phone suggestion

textField.textContentType = UITextContentTypeTelephoneNumber;


Related Topics



Leave a reply



Submit