TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Jonathan Sibley.
HTAutocompleteTextField is a subclass of UITextField that automatically displays text suggestions in real-time. This is perfect for automatically suggesting the domain as a user types an email address.
You can see it in action in the animated gif below or on Youtube, or read about it in the accompanying blog post on the HotelTonight Engineering Blog:
Create an HTAutocompleteTextField
instance exactly as as you would UITextField
. You can do eith either programmitcally or in Interface Builder. Programmatically, this looks like:
HTAutocompleteTextField *textField = [[HTAutocompleteTextField alloc] initWithFrame:CGRectMake(0,0,100,31)];
The data source is the brains of the autocomplete logic. If you just want to autocomplete email addresses, use HTAutocompleteManager
from the example project as follows:
textField.autocompleteDataSource = [HTAutocompleteManager sharedManager];
textField.autocompleteType = HTAutocompleteTypeEmail;
HTAutocompleteManager
(included in the example project) provides email address autocompletion out of the box. It comes with a list of the top email domains based on the HotelTonight customer database. You may want to tailor this list of email domains to match your own customers, or you may want to write autocomplete logic for a different type of text field (in the demo, names of colors are autocompleted).
Alternatively, you may wish to create your own data source class and user the autocompleteType
property to differentiate between fields with different data types. A HTAutocompleteTextFields
's data source must implement the following method, as part of the HTAutocompleteDataSource
protocol.
- (NSString *)textField:(HTAutocompleteTextField *)textField completionForPrefix:(NSString *)prefix ignoreCase:(BOOL)ignoreCase
You may also set a default dataSource
for all instances of HTAutocompleteTextField
. In the example project, we use a HTAutocompleteManager
singleton:
[autocompleteTextOffset setDefaultAutocompleteDataSource:[HTAutocompleteManager sharedManager]];
To adjust the position of the autocomplete label by a fixed amount, set autocompleteTextOffset
:
textField.autocompleteTextOffset = CGPointMake(10.0, 10.0);
For more dynamic positioning of the autocomplete label, subclass HTAutocompleteTextField
and override - (CGRect)autocompleteRectForBounds:(CGRect)bounds
.
To adjust the properties (i.e. font
, textColor
) of the autocomplete label, do so via the `[AutocompleteTextField autocompleteLabel] property.
textField.autocompleteLabel.textColor = [UIColor grayColor];
In addition to observing the standard UITextField
notifications (i.e. UITextFieldTextDidChangeNotification
and friends), you can use [HTAutocompleteTextField autoCompleteTextFieldDelegate]
to listen for certain events. This is particularly useful if you are collecting analytics.
Tweet the author @jonsibs, and check out HotelTonight's engineering blog: http://engineering.hoteltonight.com
Also, check out HotelTonight's other iOS open source: