使用Swift制作可点击的UILabel [英] Make Clickable UILabel Using Swift

查看:891
本文介绍了使用Swift制作可点击的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Swift在 UILabel 文本中设置特定Word可点击。



有可能吗?



如果此处有多个标签,我怎样才能检测到哪个字被按下?

解决方案

你不能用简单的标签。



github上有库。




I want to Set Particular Word clickable in UILabel text using Swift.

Is it possible?

If more than one label is here how can I detect which word is pressed?

解决方案

You can not do with the simple label.

There is library available in the github.

https://github.com/TTTAttributedLabel/TTTAttributedLabel

From this you can use the method called yourLabel.addLinkToURL()

class ViewController: UIViewController , TTTAttributedLabelDelegate{

    @IBOutlet var lbl: TTTAttributedLabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        var str : NSString = "Hello this is link"
        lbl.delegate = self
        lbl.text = str as String
        var range : NSRange = str.rangeOfString("link")
        lbl.addLinkToURL(NSURL(string: "http://github.com/mattt/")!, withRange: range)
    }

    func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
        UIApplication.sharedApplication().openURL(url)
    }
}

这篇关于使用Swift制作可点击的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆