【问题标题】:How to select all UITextview content by hold and show Copy option by programmatically iphone?如何通过保持选择所有 UITextview 内容并通过编程方式 iphone 显示复制选项?
【发布时间】:2012-09-02 22:44:31
【问题描述】:

我正在为基于消息的 iPhone 应用程序工作。在我的应用程序中,我在 UITextView 中加载了消息内容并在 UITextView 上添加了 UIImage。

现在我想选择所有 UITextView 内容,方法是按住 UITextView 并向用户显示复制选项。目前当用户hold UITextView some of the content only selecting

有人请帮我做这件事吗?提前致谢。

编辑:

在 UITableView CellForRowAtIndexPath委托

    customMessageTextView = [[MessageTextView alloc] initWithFrame:CGRectZero];
    customMessageTextView.tag = 100;
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:15]; 
    customMessageTextView.font = font;
    customMessageTextView.scrollEnabled = NO;
    customMessageTextView.delegate = self;
    customMessageTextView.dataDetectorTypes = UIDataDetectorTypeLink;
    [cell.contentView addSubview:customMessageTextView];
    [customMessageTextView sizeToFit]; 

    for (UIGestureRecognizer *recognizer in customMessageTextView.gestureRecognizers) 
    {
        if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        {
            recognizer.enabled = NO;
        }
    }


    UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(selectAllTextFromCustomMessageTextView)]; 
    [customMessageTextView addGestureRecognizer:myLongPressRecognizer];
    [myLongPressRecognizer release];

UILongPressGestureRecognizer 操作:

-(void) selectAllTextFromCustomMessageTextView
{
    NSLog(@"Select All Text Messages");
    customMessageTextView.selectedRange = NSMakeRange(0, customMessageTextView.text.length);
}

【问题讨论】:

    标签: iphone ios uitextview textselection


    【解决方案1】:

    如果我对您的理解正确,您希望在拿着UITextView(即放大镜等)时禁用标准行为。也许您甚至禁用了编辑选项。如果是这样,您只需将UILongPressGestureRecognizer 添加到您的UITextView。您可能必须禁用默认情况下内置于UITextView 中的UILongPressGestureRecognizer。你可以找到一种方法来做到这一点here

    然后在您的 UILongPressGestureRecognizer 操作方法中,您只需选择视图中的所有文本:

    [textView selectAll:self];
    

    请注意,这将打开 复制/剪切/粘贴 菜单。但是,如果您的文本视图确实禁用了用户编辑,则菜单将仅包含 复制

    【讨论】:

    • 感谢您的回答。我正在尝试你的指示。我会让你知道结果。再次感谢。
    • 它对我不起作用。我将发布我的代码。请你帮助我好吗?谢谢。
    • 你是对的,设置selectedRange 属性不起作用。我已经用一种应该有效的方法更新了我的答案(这次我已经测试过了)。
    • 那个 selectall: 不起作用,也不是 selectrange 它必须是其他东西,谁知道请发帖。
    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多