How to mention users with “@” in app like Instagram

前端 未结 2 1588

I am creating an app that has photo sharing, and I am trying to add in the functionality to mention (or tag) users. Twitter is what originally starting using \"@\" with a userna

2条回答
  •  野性不改
    2021-02-04 21:06

    You can try something like this in the UITextView delegate:

    - (void)textViewDidChange:(UITextView *)textView
    {
        _words = [self.textView.text componentsSeparatedByString:@" "];
        NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] '@'"];
        NSArray* names = [_words filteredArrayUsingPredicate:predicate];
        if (_oldArray)
        {
            NSMutableSet* set1 = [NSMutableSet setWithArray:names];
            NSMutableSet* set2 = [NSMutableSet setWithArray:_oldArray];
            [set1 minusSet:set2];
            if (set1.count > 0)
                NSLog(@"Results %@", set1);
        }
        _oldArray = [[NSArray alloc] initWithArray:names];
    }
    

    where _words, _searchResults and _oldArray are NSArrays.

提交回复
热议问题