如何自定义UILabel可点击
我想要什么:
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最简单的方法是仅将手势识别器添加到实际视图(无论是 UILabel 还是您自己的某些自定义视图)。为了使手势识别器正常工作,必须将视图设置为 userInteractionEnabled。
下面是一个示例,假设您的标签视图(或其他任何内容)称为 labelView:
在此示例中,操作消息将发送到
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:
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.
斯威夫特 4.2 版本:
Swift 4.2 Version:
斯威夫特2.0版本:
Swift 2.0 version:
听起来与他们在 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?
您还可以使用不带文本和图像的自定义按钮,在上面放置一个“隐形按钮”。
You could also just put an "invisible Button" above, by using a custom button without text and images.
我认为你可以使用 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.

通过将
UITapGestureRecognizer
添加到标签,使UIlabel
可点击。在将标签添加到 Tapgesture 之前,不要忘记为 UIlabel 启用 userinteraction
以下是示例代码:
By adding
UITapGestureRecognizer
to the label, to makeUIlabel
clickable.Before adding the label to tapgesture, don't forgot to enable userinteraction for UIlabel
Here is the sample code:
此解决方案适用于可点击的 UILabel。它不适用于文本中的选择链接。在我看来,对于像 UIButton 这样的可点击 UILabel 来说,这是一个很好的解决方案:
This solution for clickable UILabel. It isn't for select link in text. Just nice solution in my opinion for clickable UILabel like UIButton: