iOS——是否可以强制 UILabel 子类对象成为第一响应者?

发布于 2024-09-30 15:17:29 字数 174 浏览 2 评论 0原文

有什么办法可以做到这一点吗?

我尝试将以下内容放入子类中:

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

但是当我向对象发送成为FirstResponder消息时,它仍然没有成为第一响应者。

Is there any way to do this?

I tried putting the following into the subclass:

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

But when I sent the object a becomeFirstResponder message, it still didn't become the first responder.

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

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

发布评论

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

评论(3

苄①跕圉湢 2024-10-07 15:17:29

是的,这是可能的。您应该:

  1. 重写becomeFirstResponder以返回YES

  2. userInteractionEnabled设置为YES

  3. 添加 UITapGestureRecognizer 来处理点击。

  4. 从点击处理程序调用 becomeFirstResponder

您甚至可以覆盖 inputView 以在屏幕底部获取输入控件。否则就什么都没有了。

Yes, it is possible. You should:

  1. Override becomeFirstResponder to return YES.

  2. Set userInteractionEnabled to YES.

  3. Add a UITapGestureRecognizer to handle taps.

  4. Call becomeFirstResponder from the tap handler.

You can even override inputView to get a input control at the bottom of the screen. Otherwise there will be nothing.

合久必婚 2024-10-07 15:17:29

我没有明确的答案,只有一些(也许是疯狂的)想法:您是否也尝试过重写 becomeFirstResponder 以返回 YES?它的 userInteractionEnabled 是否也设置为 YES

否则,将其设为自定义 UIButton 而不是自定义 UILabel...

I have no definite answer, just a few (maybe crazy) ideas: Have you also tried to override becomeFirstResponder to return YES? Does it have userInteractionEnabled also set to YES?

Otherwise, make it a custom UIButton instead of a custom UILabel...

前事休说 2024-10-07 15:17:29

这对我有用。

子类 UILabel 并重写这些方法:

- (BOOL)canBecomeFirstResponder {
        return YES;
    }

    - (BOOL)canPerformAction:(SEL)action
                  withSender:(id)sender
    {
        return (action == @selector(copy:));
    }

    - (void)copy:(id)sender {
        [[UIPasteboard generalPasteboard] setString:self.text];
    }

将手势添加到标签以检测点击。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressDetected:)];
[_messageLabel addGestureRecognizer:longPress];

处理手势事件,呈现一个带有所需框架的 UIMenuController:

- (void)LongPressDetected:(UILongPressGestureRecognizer*)gesture {

        [_messageLabel becomeFirstResponder];
        [[UIMenuController sharedMenuController] setTargetRect:_messageLabel.bounds inView:_messageLabel];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:NO];
    }

This worked for me.

Subclass UILabel and override these methods:

- (BOOL)canBecomeFirstResponder {
        return YES;
    }

    - (BOOL)canPerformAction:(SEL)action
                  withSender:(id)sender
    {
        return (action == @selector(copy:));
    }

    - (void)copy:(id)sender {
        [[UIPasteboard generalPasteboard] setString:self.text];
    }

Add Gesture to your label to detect tap.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressDetected:)];
[_messageLabel addGestureRecognizer:longPress];

Handle gesture event, present a UIMenuController with the desired frame:

- (void)LongPressDetected:(UILongPressGestureRecognizer*)gesture {

        [_messageLabel becomeFirstResponder];
        [[UIMenuController sharedMenuController] setTargetRect:_messageLabel.bounds inView:_messageLabel];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:NO];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文