允许用户从 UILabel 选择文本进行复制

发布于 2024-10-01 04:33:21 字数 66 浏览 4 评论 0原文

我有一个 UILabel,但如何允许用户选择其文本的一部分。我不希望用户能够编辑文本,也不希望标签/文本字段具有边框。

I have a UILabel, but how can I allow the user to select a portion of it's text. I don't want the user to be able to edit the text nor the label/textfield to have a border.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-10-08 04:33:21

UILabel 不可能做到这一点。

您应该使用 UITextView 来实现这一点。只需使用 textFieldShouldBeginEditing 委托方法禁用编辑即可。

It is not possible with UILabel.

You should use UITextView for that. Just disable editing using textFieldShouldBeginEditing delegate method.

迷荒 2024-10-08 04:33:21

您使用创建一个 UITextView 并将其 .editable 设置为 NO。然后你有一个文本视图,(1)用户无法编辑(2)没有边框,(3)用户可以从中选择文本。

You use create a UITextView and make its .editable to NO. Then you have a text view which (1) the user cannot edit (2) have no border and (3) the user can select text from it.

揽清风入怀 2024-10-08 04:33:21

如果您不能或不需要使用文本视图,复制和粘贴的穷人版本就是向标签添加手势识别器,然后将整个文本复制到粘贴板。除非您使用 UITextView,否则不可能只执行一部分。

请确保让用户知道它已被复制,并且您支持单击手势和长按手势,因为它会选择用户试图突出显示文本的一部分。下面是一些示例代码,可帮助您入门:

创建标签时在标签上注册手势识别器:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)];
                [myLabel addGestureRecognizer:tap];
                [myLabel addGestureRecognizer:longPress];
                [myLabel setUserInteractionEnabled:YES];

接下来处理手势:

- (void) textPressed:(UILongPressGestureRecognizer *) gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
        [gestureRecognizer.view isKindOfClass:[UILabel class]]) {
        UILabel *someLabel = (UILabel *)gestureRecognizer.view;
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:someLabel.text];
        ...
        //let the user know you copied the text to the pasteboard and they can no paste it somewhere else
        ...
    }
}

- (void) textTapped:(UITapGestureRecognizer *) gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
        [gestureRecognizer.view isKindOfClass:[UILabel class]]) {
            UILabel *someLabel = (UILabel *)gestureRecognizer.view;
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            [pasteboard setString:someLabel.text];
            ...
            //let the user know you copied the text to the pasteboard and they can no paste it somewhere else
            ...
    }
}

A poor man's version of copy and paste, if you cannot, or don't need to use a text view, would be to add a gesture recognizer to the label and then just copy the entire text to the pasteboard. It's not possible to do just a portion unless you use a UITextView

Make sure you let the user know it's been copied and that you support both a single tap gesture as well as a long press, as it will pick up users trying to highlight a portion of text. Here is a bit of sample code to get you started:

Register the gesture recognizers on your label when you create it:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)];
                [myLabel addGestureRecognizer:tap];
                [myLabel addGestureRecognizer:longPress];
                [myLabel setUserInteractionEnabled:YES];

Next up handle the gestures:

- (void) textPressed:(UILongPressGestureRecognizer *) gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
        [gestureRecognizer.view isKindOfClass:[UILabel class]]) {
        UILabel *someLabel = (UILabel *)gestureRecognizer.view;
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:someLabel.text];
        ...
        //let the user know you copied the text to the pasteboard and they can no paste it somewhere else
        ...
    }
}

- (void) textTapped:(UITapGestureRecognizer *) gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
        [gestureRecognizer.view isKindOfClass:[UILabel class]]) {
            UILabel *someLabel = (UILabel *)gestureRecognizer.view;
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            [pasteboard setString:someLabel.text];
            ...
            //let the user know you copied the text to the pasteboard and they can no paste it somewhere else
            ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文