Almudhafar
Almudhafar

Reputation: 887

UIMenuController and UITextView selected text

first i am using this UITextView class

textViewClass.h

@interface textViewClass : UITextView

textViewClass.m

@implementation textViewClass


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];

        if (!items) items = [[NSMutableArray alloc] init];

        UIMenuItem *menuItem;
        menuItem = [[UIMenuItem alloc] initWithTitle:@"Add note" action:@selector(doSomethingHere:)];
        [items addObject:menuItem];

        [[UIMenuController sharedMenuController] setMenuItems:items];

    }
    return self;
}

- (void)doSomethingHere:(UITextView *)textView
{
    NSLog(@"add note: ");

}

And in MainViewController.h

#import "textViewClass.h"

@interface testViewController : UIViewController <UITextViewDelegate>

@property (strong, nonatomic) IBOutlet textViewClass *myTextView;

And in MainViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.myTextView setEditable:NO];
    [self.myTextView setDelegate:self];

    [self.myTextView setText:@"Lorem ipsum dolor sit er elit lamet, 
                              consectetaur cillium adipisicing pecu, 
                              sed do eiusmod tempor incididunt ut labore 
                              et dolore magna aliqua. Ut enim ad minim 
                              veniam, quis nostrud exercitation ullamco 
                              laboris nisi ut aliquip ex ea commodo consequat."];
}


#pragma mark - UITextView Delegate

- (void)textViewDidChangeSelection:(UITextView *)textView
{

    NSString *string = [self.myTextView textInRange:textView.selectedTextRange];

    NSLog(@"%@", string);



}

I want to show 'Add note' when i selected some text in myTextView and copy this selection to clipboard and do some action as i want.

Upvotes: 4

Views: 2705

Answers (1)

Goppinath
Goppinath

Reputation: 10739

hmm you need two corrections. The first is:

you have to implement the UIResponder method like this in your custom UITextView class.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(doSomethingHere:) || action == @selector(copy:)) {

        if (self.selectedRange.length > 0) {

            return YES;
        }
    }
    return NO;
}

here you are filtering the actions with the code which has been defined under the UIResponderStandardEditActions informal protocol.

if (action == @selector(doSomethingHere:) || action == @selector(copy:))

if you only need yourAction only then do

if (action == @selector(doSomethingHere:))

Second correction: if you implement the protocol like this

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    return YES;
}

you will see all possible commands by default therefor: Either filter it by selector or add your own command only like this,

UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:@"Add note" action:@selector(doSomethingHere:)];
[items addObject:menuItem];

[[UIMenuController sharedMenuController] setMenuItems:@[menuItem]];

Hope it will help you...

NB: For more information please check NSHipster

Upvotes: 3

Related Questions