devopsinthecloud
devopsinthecloud copied to clipboard
Trigger mentions programmatically?
Is there a way to enter mention-pick state by code? I.e. simulate action key of explicit mention.
This is requirement I have to place '#' button in convenient place. I tried
[textView insertText...]
or
[textView replaceRange:withText:]
with no success. I.e. control character is being inserted to text, but no mention autocomplete list shows up.
For those who still try to trigger it programatically, here's how I did it
- Extend HKWTextView and add in header file the shouldChangeTextInRange method (it will give you warning because it's reflexion ) @interface MYHKWTextView : HKWTextView
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)replacementText;
@end
-
In MYHKWTextView.m file DO NOT implement this method
-
In the context you want to trigger the mentions view programatically, e.x. on the click of a button
-
(IBAction)tagButtonPressed:(id)sender {
//hide the placeholder label, if you have one if([self.shareTextView.text length] == 0) { self.placeholderLabel.hidden = YES; }
//add a space suffix if needed if (![self.shareTextView.text hasSuffix:@" "]) { NSAttributedString *oldAttrString = self.shareTextView.attributedText;
NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithAttributedString:oldAttrString]; [newString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; [self.shareTextView setAttributedText:newString];
// we need attributed string here to keep the old added mentions }
//mention the guys you want to annoy most (here's where the magic happens )
NSRange range = NSMakeRange(0, 0);
[self.shareTextView textView:self.shareTextView shouldChangeTextInRange:range replacementText:@"@"];
} Enjoy
I solved it by adding adding the following 'showChooser' method to HKWMentionsPlugin. This approach assumes prior to calling it you have inserted your control character (@ in this case) at the end of the text view. You can most likely do without this if you pass 'No' to 'alreadyInserted', but I haven't verified that as it isn't my particular use case.
- (void)showChooser {
[self beginMentionsCreationWithString:@"" alreadyInserted:YES usingControlCharacter:YES controlCharacter:[@"@" characterAtIndex:0]];
}
Not sure if this will do much for a 5 year old issue or not, but I'll add myself into the list of people that would really like to be able to trigger a mention programmatically after a user taps a button and isn't able to get the above suggestions to work.