UILabel 中的可点击用户名和主题标签(不是 UIWebView!)

Clickable username and hashtag in UILabel (Not UIWebView!)

这个想法是例如有一个 UILabel(就像在 Instagram 中一样) "User1 started following User2" 我希望当我们单击文本块 'User1' 或 'User2' 时它会执行一些操作(不像往常一样在 UIWebView 中打开 link)

尝试了 TTTAttributedLabel,没有找到完全适合我的东西。

只需制作一个新按钮即可。它可以做标签可以做的一切,并且还有动作。您可以使用以下方法轻松更新按钮的文本:

exampleButton.setTitle("example", forState: .Normal)

您可以添加这样的操作:

    @IBAction func myExample(sender: UIButton) {

    //do action

    }

希望对您有所帮助。

使用一种方法将强制您制作其中两个(用户 1 和用户 2)并在每次绘制时将其对齐。我认为最好制作一个 UILabel 的类别(或 Swift 扩展)来获取它的框架并添加一个具有该框架边界的手势识别器

如果您还没有找到,您也可以尝试 ActiveLabel.swift,这是 UILabel 的替代品 支持用 Swift.

编写的标签 (#)、提及 (@) 和 URL (http://)

这是一个简单的例子:

import ActiveLabel

let label = ActiveLabel()

label.text = "This is a post with #hashtags and a @userhandle."
label.hashtagColor = .blueColor()
label.handleHashtagTap { print("Success. You just tapped the \([=10=]) hashtag") }

免责声明:我是图书馆的作者。