【问题标题】:iOS Propagate UITextView text selection to the UITableviewCell to display action menu itemsiOS 将 UITextView 文本选择传播到 UITableviewCell 以显示操作菜单项
【发布时间】:2017-11-24 13:10:44
【问题描述】:

在尝试了 SO 的许多不同答案后,我决定提出自己的问题。

我有一个包含 UITextView 的 UITableViewCell。 textview 可以包含纯文本或链接。我基本上希望链接是可选择的,但长按链接以外的任何地方都应该显示菜单操作,例如复制和粘贴。

到目前为止我已经尝试过。我已经继承了UITextView

-(BOOL)canBecomeFirstResponder {
    return NO;
}

我设置了bodyTextView.selectable = NO。这确实提供了所需的效果,但链接变得无法选择。

一旦我更改为bodyTextView.selectable = YES,链接是可选择的,但在其中只有文本的文本视图上长按,选择没有操作的文本,如图所示。

理想情况下,我只想选择链接。我也试过了

- (void)textViewDidChangeSelection:(UITextView *)textView {
   if(!NSEqualRanges(textView.selectedRange, NSMakeRange(0, 0))) {
       textView.selectedRange = NSMakeRange(0, 0);
   }
}

这肯定会删除选择,但仍然无法显示弹出操作菜单。任何帮助,将不胜感激。

【问题讨论】:

    标签: ios objective-c uitableview uitextview


    【解决方案1】:

    请通过此链接创建超链接,一旦您创建了超链接,默认情况下会注意可选择的基于内容的操作。

    UITextView with hyperlink text

    【讨论】:

      【解决方案2】:

      经过多次尝试,我使用了以下方法。在 UITextView 的子类中,我检查了用户是否正在触摸链接,如果没有将 textview 设置为不可选择

      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[touches allObjects] lastObject];
        CGPoint tapLocation = [touch locationInView:self];
        UITextPosition *textPosition = [self closestPositionToPoint:tapLocation];
        NSDictionary *attributes = [self textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
        NSURL *url = attributes[NSLinkAttributeName];
        if (url) {
            self.selectable = YES;
        }else {
            self.selectable = NO;
        }
        [self.superview touchesBegan:touches withEvent:event];
      

      }

      这样,如果用户触摸了 textview 的非链接部分,它就会传递给 superview。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        相关资源
        最近更新 更多