textView自定义选择文字后的菜单

#pragma mark - 自定义选择文字后的菜单
- (void)textViewZiDingYiCaiDan
{
    textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 250, 40)];
    
    NSString *baiduUrl = @"www.baidu.com";
    
    textView.text = baiduUrl;
    
    [self.view addSubview:textView];
    
    
    ///
    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"分享到我的主页" action:@selector(fun)];
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObject:menuItem]];
}

#pragma mark - Actions
// 然后还得在代码里加上一个方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(fun) || action == @selector(copy:)) {
        
        if (textView.selectedRange.length > 0) {
            
            return YES;
        }
    }
    
    return NO;
}

- (void)fun
{
    NSLog(@"%@",[textView.text substringWithRange:textView.selectedRange]);
    
}

posted on 2015-03-20 09:43  JZLOY  阅读(335)  评论(0编辑  收藏  举报

导航