devopsinthecloud icon indicating copy to clipboard operation
devopsinthecloud copied to clipboard

Trigger mentions programmatically?

Open tomekc opened this issue 9 years ago • 3 comments

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.

tomekc avatar Jul 08 '15 14:07 tomekc

For those who still try to trigger it programatically, here's how I did it

  1. 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

  1. In MYHKWTextView.m file DO NOT implement this method

  2. 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

daniel-mihai avatar Apr 26 '17 15:04 daniel-mihai

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]];
}

chadpod avatar May 16 '17 19:05 chadpod

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.

KrisConrad avatar Sep 11 '20 15:09 KrisConrad