如何自定义UILabel可点击

发布于 2024-10-13 02:38:04 字数 628 浏览 4 评论 0原文

我想要什么:

在 iPhone 应用程序中,我想在 tableView 中显示信息。 在每个单元格中,文本如下:John 最近听音乐 abcdefg.mp3。 如果需要,文本可以有两行。

在文本中,a.mp3 应该是可点击的,因此当用户触摸 abcdefg.mp3 部分时,将调用另一个页面。当用户触摸abcdefg.mp3时,也会产生一些效果,就像触摸按钮一样。

我所做的:

我计算文本的框架,并为 abcdefg.mp3 使用 UIButton。

我的问题:

有时 abcdefg.mp3 可能是多行的,例如:

abc 位于第一行末尾

defg.mp3 在第二行。

这种情况我该怎么办?

我已经搜索过:在 UILabel 的 NSAttributedString 中创建可点击的“链接”? 但是我认为它不适合这里,因为可单击的文本全部位于示例中的一行中。

What I want:

In an iPhone app, I'd like to show information in a tableView.
In each cell, the text is like: John recently listen to music abcdefg.mp3.
and if needed, the text can have two lines.

In the text, a.mp3 should be clickable so when the user touches the abcdefg.mp3 part, another page will be invoked. When user touches abcdefg.mp3, it will also have some effects, just like touching a button.

What I do:

I calculate the frame of the text, and I use a UIButton for abcdefg.mp3.

My Problem:

Sometimes abcdefg.mp3 may be in multiline, like:

abc is at the end of the first line

defg.mp3 is in second line.

What should I do in this case?

I've already searched about: Create tap-able "links" in the NSAttributedString of a UILabel?
However I think it is not suitable here as the clickable text is all in one line in the sample.

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

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

发布评论

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

评论(8

优雅的叶子 2024-10-20 02:38:04

最简单的方法是仅将手势识别器添加到实际视图(无论是 UILabel 还是您自己的某些自定义视图)。为了使手势识别器正常工作,必须将视图设置为 userInteractionEnabled。

下面是一个示例,假设您的标签视图(或其他任何内容)称为 labelView:

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];

在此示例中,操作消息将发送到 self ,并且该消息将定义为

- ( void)userTappedOnLink:(UIGestureRecognizer*)gestureRecognizer;

这与连接任何其他 UIControl 子类(例如按钮)的工作方式相同。

其他注意事项:不要尝试将相同的手势识别器添加到多个视图,这是行不通的。不要将手势识别器的多个副本添加到多个视图(它不会替换它们,它只是将它们堆叠起来并浪费内存)。您应该在最初创建和配置视图时添加手势识别器。

有关更多信息,请参阅 UIGestureRecognizer 的文档。

The most simple way is to just add a gesture recognizer to the actual view (be it a UILabel or some custom view of your own). In order for the gesture recognizer to work, the view must be set userInteractionEnabled.

Here's an example, assuming that your label view (or whatever it is) is called labelView:

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];

In this example, an action message will be sent to self and the message would be defined as

- (void)userTappedOnLink:(UIGestureRecognizer*)gestureRecognizer;

This works the same as wiring up any other UIControl subclass, such as a button.

Other notes: don't try to add the same gesture recognizer to multiple views, it won't work. Don't add more than one copy of the gesture recognizer to multiple views (it doesn't replace them, it just stacks them up and wastes memory). You should add the gesture recognizer when you initially create and configure your view.

For more information, see the documentation for UIGestureRecognizer.

初雪 2024-10-20 02:38:04

斯威夫特 4.2 版本:

var labelView = UILabel()
let gesture = UITapGestureRecognizer(target: self, action: #selector(userTappedOnLink))
// if labelView is not set userInteractionEnabled, you must do so
labelView.isUserInteractionEnabled = true
labelView.addGestureRecognizer(gesture)


@objc func userTappedOnLink() {
    print("clicked!")
}

Swift 4.2 Version:

var labelView = UILabel()
let gesture = UITapGestureRecognizer(target: self, action: #selector(userTappedOnLink))
// if labelView is not set userInteractionEnabled, you must do so
labelView.isUserInteractionEnabled = true
labelView.addGestureRecognizer(gesture)


@objc func userTappedOnLink() {
    print("clicked!")
}
银河中√捞星星 2024-10-20 02:38:04

斯威夫特2.0版本:

  func userTappedOnLink() {
    print("clicked!")
  }

///tap and link to FB page
let gesture = UITapGestureRecognizer(target: self, action: "userTappedOnLink")
// if labelView is not set userInteractionEnabled, you must do so
lblStaff.userInteractionEnabled = true
lblStaff.addGestureRecognizer(gesture)

Swift 2.0 version:

  func userTappedOnLink() {
    print("clicked!")
  }

///tap and link to FB page
let gesture = UITapGestureRecognizer(target: self, action: "userTappedOnLink")
// if labelView is not set userInteractionEnabled, you must do so
lblStaff.userInteractionEnabled = true
lblStaff.addGestureRecognizer(gesture)
独自←快乐 2024-10-20 02:38:04

听起来与他们在 Twitteriffic 中使用 Fancy UILabels 所完成的工作类似。基本上,Craig Hockenberry 制作了自己的定制“数据检测器”,以便他可以在标签和多行文本中插入链接。请参阅此问题:有没有办法创建自定义 UIDataDetectorTypes?

Sounds similar to what they accomplished in Twitteriffic with Fancy UILabels. Basically, Craig Hockenberry made his own custom "data detector", so that he could insert links within labels and multi-line text. See this question: Is there a way to create custom UIDataDetectorTypes?

兔小萌 2024-10-20 02:38:04

您还可以使用不带文本和图像的自定义按钮,在上面放置一个“隐形按钮”。

在此处输入图像描述

You could also just put an "invisible Button" above, by using a custom button without text and images.

enter image description here

奶气 2024-10-20 02:38:04

我认为你可以使用 textView 并禁用滚动。我对我的项目做了同样的事情,我需要指向网站地址。只需取消选中检查器窗口中“滚动视图”部分中的屏幕截图所示的所有内容即可。
禁用滚动

I think you can use textView and diable the scrolling. I did same for my project where I need to point to website addresses. just uncheck everything as shown in screenshot in 'scroll view' section in inspector window.
disable scrolling

呆头 2024-10-20 02:38:04

通过将 UITapGestureRecognizer 添加到标签,使 UIlabel 可点击。

在将标签添加到 Tapgesture 之前,不要忘记为 UIlabel 启用 userinteraction

以下是示例代码:

//Tap Gesture
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapped:)];
//Adding userinteraction for label
[labelName setUserInteractionEnabled:YES];
//Adding label to tap gesture
[labelName addGestureRecognizer:gesture];

By adding UITapGestureRecognizer to the label, to make UIlabel clickable.

Before adding the label to tapgesture, don't forgot to enable userinteraction for UIlabel

Here is the sample code:

//Tap Gesture
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapped:)];
//Adding userinteraction for label
[labelName setUserInteractionEnabled:YES];
//Adding label to tap gesture
[labelName addGestureRecognizer:gesture];
可是我不能没有你 2024-10-20 02:38:04

此解决方案适用于可点击的 UILabel。它不适用于文本中的选择链接。在我看来,对于像 UIButton 这样的可点击 UILabel 来说,这是一个很好的解决方案:

#import <UIKit/UIKit.h>

@protocol ClickableLablelDelegate <NSObject>

@required
- (void)onClickLabel:(UILabel *) label;

@end

@interface ClickableLable : UILabel

@property (nonatomic, weak) id <ClickableLablelDelegate> delegate;

@end


#import "ClickableLable.h"

@implementation ClickableLable

-(void)awakeFromNib
{
    [super awakeFromNib];

    [self setUserInteractionEnabled:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor lightGrayColor]];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor clearColor]];

    if ([_delegate respondsToSelector:@selector(onClickLabel:)]) {
        [_delegate onClickLabel:self];
    }
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor clearColor]];
}

This solution for clickable UILabel. It isn't for select link in text. Just nice solution in my opinion for clickable UILabel like UIButton:

#import <UIKit/UIKit.h>

@protocol ClickableLablelDelegate <NSObject>

@required
- (void)onClickLabel:(UILabel *) label;

@end

@interface ClickableLable : UILabel

@property (nonatomic, weak) id <ClickableLablelDelegate> delegate;

@end


#import "ClickableLable.h"

@implementation ClickableLable

-(void)awakeFromNib
{
    [super awakeFromNib];

    [self setUserInteractionEnabled:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor lightGrayColor]];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor clearColor]];

    if ([_delegate respondsToSelector:@selector(onClickLabel:)]) {
        [_delegate onClickLabel:self];
    }
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundColor:[UIColor clearColor]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文