这是一个无耻的分支,由Krelborn/KILabel演变而来,用手动添加链接代替自动链接检测。
使用TapLabel.LinkContentName
将链接应用到富文本,并使用TapLabel.SelectedForegroudColorName
来指定所选前景颜色。TapLabel不会应用任何其他自定义颜色到文本。您必须自己定义。
let text = NSMutableAttributedString(string: "something contains a link http://google.com")
text.addAttribute(TapLabel.LinkContentName,
value: "test",
range: NSMakeRange(26, 42))
text.addAttribute(NSForegroundColorAttributeName,
value: UIColor.blueColor(),
range: NSMakeRange(26, 42))
text.addAttribute(TapLabel.SelectedForegroudColorName,
value: UIColor.redColor(),
range: NSMakeRange(26, 42))
let label = TapLabel(frame: CGRect(x: 20, y: 40, width: 280, height: 100))
label.attributedText = text
必须分配label上的代理属性才能实现点击操作。
func tapLabel(tapLabel: TapLabel, didSelectLink link: String)
当指定了TapLabel.LinkContentName
的文本被点击时,将会被调用一次。其中link
将等于TapLabel.LinkContentName
的值。