Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Clickable UILabel Using Swift

Tags:

ios

uilabel

swift

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?

like image 513
vivek malani Avatar asked Aug 14 '15 12:08

vivek malani


3 Answers

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)
like image 117
Pavel Sharanda Avatar answered Oct 22 '22 13:10

Pavel Sharanda


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)
    }
}

enter image description here

like image 26
Ashish Kakkad Avatar answered Oct 22 '22 12:10

Ashish Kakkad


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)
    }
like image 1
krish Avatar answered Oct 22 '22 11:10

krish