简体   繁体   中英

Suggestion of hashtags in UITextField

I'm building a to-do list and when the user creates a new task, I use a UITextfield for entering task name. What I would like to provide is when the user type # in that UITextField I present a list of tags already created by the user or if the Tag doesn't exist I would create a new one and assign it to that new task.

I have already searched in here and I can't find a similar question with a proper awnser. I can't find a way to only present the suggestions when the # is pressed.

By the way, sorry about my bad english.

Set up your containing view controller as a UITextFieldDelegate. With that in place, you can implement

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

This is called when a digit is typed into the field, so check the replacement string against the hash tag, and create the drop down as appropriate:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if([string isEqualToString:@"#"]) {
        [containingViewController showDropdownListFromElement:textField];

        // Now consume the hash tag by not allowing the addition
        return NO;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM