Link within label in Swift
Asked Answered
D

4

9

I would like to do that in my app :

enter image description here

The label is like : username comment

I don't know how to add the "button" within the label; I found this library but I'm not sure it will work? https://github.com/optonaut/ActiveLabel.swift

Maybe use it by creating a regex for the first word? What do you think?

Drollery answered 5/6, 2017 at 9:34 Comment(3)
Active label is working fine.Whitcomb
@Whitcomb can u provide some code to make a user nickname clickable? without "@".Berny
@VladPulichev,@Drollery see the answer posted by me recently. :)Whitcomb
S
15

For such a case, instead of adding it as a UILabel component, I would rather use UITextView, because it has dataDetectorTypes property:

The types of data converted to tappable URLs in the text view.

You can use this property to specify the types of data (phone numbers, http links, and so on) that should be automatically converted to URLs in the text view. When tapped, the text view opens the application responsible for handling the URL type and passes it the URL. Note that data detection does not occur if the text view's isEditable property is set to true.

So, you can implement it as:

// as mentioned in the documentation, make sure to let it be uneditable:
textView.isEditable = false
textView.dataDetectorTypes = .link

For your case, I assume that it shall be .link. I would also suggest to check the other options for the UIDataDetectorTypes.

Sun answered 5/6, 2017 at 9:58 Comment(1)
UITextView introduces complexities since its not just a view but its a UIScrollView.Mayoralty
W
6

Use Active-Label https://github.com/optonaut/ActiveLabel.swift Make a UILabel named label

 let customType = ActiveType.custom(pattern: "\\sjohncena\\b") 
 label.enabledTypes.append(customType3)
 label.handleCustomTap(for: customType) { self.alert("Custom type", message: $0) }

 func alert(_ title: String, message: String) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }

where johncena is string which is clickable. see https://github.com/optonaut/ActiveLabel.swift/blob/master/ActiveLabelDemo/ViewController.swift

Whitcomb answered 6/6, 2017 at 6:0 Comment(1)
I will try to do it in the evening. I will uparrow, if it helps. thx!Berny
S
2

Swift 5

You can use UITextView and can simply use this way.

class ViewController: UIViewController, UITextViewDelegate {
        @IBOutlet var textView: UITextView!
    
        override func viewDidLoad() {
            let attributedString = NSMutableAttributedString(string: "Want to learn iOS? You should visit the best source of free iOS tutorials!")
            attributedString.addAttribute(.link, value: "https://www.hackingwithswift.com", range: NSRange(location: 19, length: 55))
    
            textView.attributedText = attributedString
        }
    
        func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
            UIApplication.shared.open(URL)
            return false
        }
    }
Stand answered 30/9, 2021 at 12:57 Comment(0)
R
0

You can use TTTAttributedLabel to make a particular part or word tappable in a UILabel.

For example

label.text = @"Fork me on GitHub! (https://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range];

In addition to hyperlinks you can add custom links also to a particular part or word and that word becomes tappable and on tapping that word delegate method didSelectLinkWithURL gets called and you can check for that link inside that function

(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    NSLog(@"link %@", [url absoluteString]);
    NSLog(@"whole label %@", label);
}
Run answered 5/6, 2017 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.