首页 > 解决方案 > 从 UITableView 中的 UITextView 复制文本

问题描述

我有一个表格视图单元格,它有一个图像视图、一个 UILabel 和一个 TextView。我希望用户在长按 TextView 时能够从 TextView 中选择文本。

我为单元格创建了一个手势识别器,当用户长按单元格时,手势识别器被调用,但我没有菜单或光标让用户能够选择文本然后复制它. 下面是将长按附加到 UITableView 单元格的代码

  UILongPressGestureRecognizer* longPressRecognizer =     [[UILongPressGestureRecognizer alloc] initWithTarget:self   action:@selector(LongPressgesture:)];
 [tableView addGestureRecognizer:longPressRecognizer];

我也有以下代码被调用

- (void)LongPressgesture:(UILongPressGestureRecognizer *)gesture{
    if (gesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended .................");
     }
    else {
        NSLog(@"Long press detected .....................");
    }
}

该函数被调用,所以我在输出日志上看到消息“检测到长按”,但我没有得到复制文本的菜单。我尝试在长按部分创建一个菜单,如下所示:

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyText:)];

    UIMenuController *menuCont = [UIMenuController sharedMenuController];

    [menuCont setTargetRect:CGRectMake(10, 100, 400, 400) inView:tableview];

    menuCont.arrowDirection = UIMenuControllerArrowDown;
    menuCont.menuItems = [NSArray arrayWithObject:menuItem];
    [menuCont setMenuVisible:YES animated:YES];

但是没有调用复制文本功能,也没有菜单。

谢谢!

标签: objective-cuitableviewuitextview

解决方案


推荐阅读