5

I am starting to work with text fields, and I want to implement a functionality with a custom autocomplete.

In a lot of blogs and answer, implement it with UITables to show the autocomplete, but I do not need this, I need to create a autocomplete like a iPhone function, where show a pop out with the filter but only with my array of words.

For example.

my_array = @{"apple","banana","pear","orange","berry"}

When I type "app" the text field only need to show me "apple" but not complete the text in the text field, should show the pop up with the autocomplete.

Something like this but only with my array.

enter image description here

nobody
  • 19,814
  • 17
  • 56
  • 77
user2720097
  • 509
  • 5
  • 15
  • 1
    Check out https://github.com/hoteltonight/HTAutocompleteTextField – Milo May 13 '14 at 02:41
  • thanks for your answer, but it library complete the word in the textfield, and I need that the functionality suggest a word and the user decide if use it or not – user2720097 May 13 '14 at 02:57
  • The example you showed is the default Auto Correct that comes with iOS. I don't believe its customizable without violating Apple guideline. – Milo May 13 '14 at 03:13
  • so, do you think that is not possible create something like this ? – user2720097 May 13 '14 at 03:26
  • You'd have to implement your own logic and a UIView to display and things like that but it's certainly possible – Milo May 13 '14 at 03:28
  • 1
    I asked the same question: http://stackoverflow.com/questions/14212933/implement-auto-completion-on-ipad. This might help you. Basic idea: TableView beneath your TextField that will be updated at each keystroke. – Marc May 13 '14 at 07:55

2 Answers2

5

You can build this behaviour yourself using the UITextFieldDelegate methods ( implement the delegate in your UIView

@interface someViewController : UIViewController <UITextFieldDelegate> 

In doing this you get access to whatever the user has typed in

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Where you can compare it to the items in your array and display another UIView or a custom button that when selected populates your text field.

Don't forget to tell your textfield who it's delegate should be, probably in your viewDidLoad method, but can also be done in the xib view

myTextField.delegate = self;

I know this seems laborious but it will be extremely gratifying. Here's the apple doc for the UITextViewDelegate

rihekopo
  • 3,241
  • 4
  • 34
  • 63
Mindeater
  • 322
  • 3
  • 9
  • Thanks for the answer, finally, create a custom UIView and I did all of functionalities in the method shouldChangeCharactersInRange – user2720097 May 14 '14 at 15:24
1

I've implemented a custom auto complete textfield few days ago and the raywenderlich tutorial was helpful.

http://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values

You probably need to implement your own table view to make it look like the screen you attached. The short tutorial will give you an idea how it should be done. I will post my code tomorrow if you want.

HMHero
  • 2,333
  • 19
  • 11