Make Clickable UILabel Using Swift
11,966
Solution 1
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)
}
}
Solution 2
I'd like to share my library https://github.com/psharanda/Atributika
It contains modern replacement of TTTAtributedLabel + powerful set of methods to detect and style different stuff like tags, hashtags, mentions etc (everything of that can be clickable)
Some code to show how it works:
let link = Style
.font(.boldSystemFont(ofSize: 14))
.foregroundColor(.black)
.foregroundColor(.red, .highlighted)
let tos = link.named("tos")
let pp = link.named("pp")
let all = Style
.font(.systemFont(ofSize: 14))
.foregroundColor(.gray)
let text = "<tos>Terms of Service</tos> and <pp>Privacy Policy</pp>"
.style(tags: tos, pp)
.styleAll(all)
let tosLabel = AttributedLabel()
tosLabel.textAlignment = .center
tosLabel.attributedText = text
tosLabel.onClick = { label, detection in
switch detection.type {
case .tag(let tag):
switch tag.name {
case "pp":
print("Privacy Policy clicked")
case "tos":
print("Terms of Service clicked")
default:
break
}
default:
break
}
}
view.addSubview(tosLabel)
Solution 3
SWIFT 3.0
privacyLabel.delegate = self
let strPolicy : NSString = "Agree to the Terms & Conditions"
privacyLabel.text = strPolicy as String
let range1 : NSRange = strPolicy.range(of: "Terms & Conditions")
privacyLabel.addLink(to: URL(string: "http://Terms.com")!, with: range1)
func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
print("url \(url)")
// UIApplication.sharedApplication().openURL(url)
}
Author by
vivek malani
Updated on July 10, 2022Comments
-
vivek malani almost 2 years
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?
-
JAY RAPARKA over 8 yearsadd tap gesture to label you can get this with it's action.
-
-
Ashish Kakkad over 8 years@Daij-Djan Ok, sir. can you please post as answer ? So the questioner will get correct result and answer too.
-
Daij-Djan over 8 yearsIll try. I haven't done this :) Ill look at it right away
-
Sulthan over 8 yearsI believe the simplest solution is to use 1-2 labels and a button. Autolayout makes it really easy.
-
Daij-Djan over 8 yearsit isn't as easy as I figured because UILabel doesn't give me the character at the location I clicked. - thought it would since UITextView does... my bad @AshishKakkad
-
Ashish Kakkad over 8 years@Daij-Djan No problem :) I know that UITextView Does.
-
vivek malani over 8 yearsHow to Expand TTTAttributedLabel in swift
-
Ashish Kakkad over 8 years@vivekmalani multiline text?
-
vivek malani over 8 yearsYes I Required Multiline Label Please Help me
-
Ashish Kakkad over 8 years@vivekmalani Just set. Number of lines zero and set word wrap on.
-
vivek malani over 8 yearsLet us continue this discussion in chat.