Textview with Suggestions List in iOS

旧时模样 提交于 2019-12-10 04:21:19

问题


I am trying to implement a textView in which when user starts typing(Let's say names) it would show up the suggestions and when they click them it gets added to the textView than user presses comma and again the same functionality for another name....

And at the end the text in the textView should look like this...

Aron,Maria,Alex,Cassie

Can any one suggest me How can I achieve this? (Its somewhat similar to adding the "Tags" while posting this question!!!)

Thanks.


回答1:


You can use a NSTokenField replacement there is some libraries here :

tokenField libraries




回答2:


Following link may help you: http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values

Follow the same flow. To get autocomplete suggestions after comma modify the delegate method as found below.

- (BOOL)textField:(UITextField *)textField 
    shouldChangeCharactersInRange:(NSRange)range 
    replacementString:(NSString *)string {
  autocompleteTableView.hidden = NO;

  NSString *names = [NSString stringWithString:textField.text];
  NSArray* arr = [names componentsSeparatedByString:@","];
  NSString *subString = [arr lastObject];
  substring = [substring 
    stringByReplacingCharactersInRange:range withString:string];
  [self searchAutocompleteEntriesWithSubstring:substring];
  return YES;
}

Provide a NSMutableArray named 'allNames' which contains all the names you want to display in the suggestion list and use it as the following:

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
  [autocompleteUrls removeAllObjects];
  for(NSString *curString in allNames) {
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
      [autocompleteUrls addObject:curString];  
    }
  }
  [autocompleteTableView reloadData];
}

When the user clicks the suggestions display the name by appending with previously entered names.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  // set the textField.text by appending this name to already entered names

}



回答3:


I didn't see anything like this in mobile app. You can look for libraries. But if you want to make it yourself I would advice you to use invisible tableView. When user starts typing name you should fetchData and show in tableView under textView. It's not hard.




回答4:


A relatively easy way I can think of to achieve this functionality would be to add an input accessory view to your keyboard, which will offer suggestions.

You wouldn't have to tamper with the TextField itself, nor would you need to incorporate the suggestions into the remainder of your apps layout.

The accessory view could, for example, be given a reference to the textfield and listen to input by:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextFieldTextDidChangeNotification object:textFieldWithSuggestions];

It would feature a method -(void)textChanged; in which you would have the opportunity to split the existing text into components, using comma or whatever symbol as a separator, and then use the last text fragment to perform your search for possible completions.

It may present these suggestions as a row of buttons for example (maybe even in a side-scrolling scrollview to allow for many suggestions) and if one gets pushed, update the textfields text by replacing the last text segment with the completed string.

To keep track of which button stands for which suggestion, just give them tags according to the indices of your search results. This way, you'll need only one method as a target for the buttons, too.




回答5:


If you want to some library code then you can go for this https://github.com/hoteltonight/HTAutocompleteTextField which will help you



来源:https://stackoverflow.com/questions/16686379/textview-with-suggestions-list-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!