How to make url / interactive UILabel with a click?

I want to create an interactive tag in my application that will lead me to the Safari webpage. I also want the user to be able to call numbers only by clicking on them?

Thank you for your advice.

+109
url objective-c iphone uilabel
May 21 '12 at 8:39
source share
8 answers

You can use UITextView and select "Discovery for links", "Phone numbers" and other things in the inspector.

+127
May 21 '12 at 8:43
source share

Use a UITextView instead of a UILabel , and it has the property to convert text to hyperlink

  Obejctive C: yourTextView.editable = NO; yourTextView.dataDetectorTypes = UIDataDetectorTypeAll; Swift: yourTextView.editable = false; yourTextView.dataDetectorTypes = UIDataDetectorTypes.All; 

See the documentation for more details. This will automatically detect the links.

Hope this helps you. Thank:)

+93
May 21 '12 at 18:46
source share

https://github.com/mattt/TTTAttributedLabel

This is definitely what you need. You can also apply attributes to your label, such as underlining, and apply different colors to it. Just check the instructions for interactive URLs.

Basically you do something like the following:

 NSRange range = [label.text rangeOfString:@"me"]; [label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring 
+28
May 21 '12 at 8:53 a.m.
source share

You can customize the UIButton and setText as you want and add a method with this.

  UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom]; [sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)]; [sampleButton setTitle:@"URL Text" forState:UIControlStateNormal]; [sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; [sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:sampleButton]; -(void)buttonPressed:(id)sender{ // open url } 
+12
May 21 '12 at 8:44 am
source share

If you want this to be handled using a UILabel rather than a UITextView, you can subclass UILabel like this:

 class LinkedLabel: UILabel { fileprivate let layoutManager = NSLayoutManager() fileprivate let textContainer = NSTextContainer(size: CGSize.zero) fileprivate var textStorage: NSTextStorage? override init(frame aRect:CGRect){ super.init(frame: aRect) self.initialize() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.initialize() } func initialize(){ let tap = UITapGestureRecognizer(target: self, action: #selector(LinkedLabel.handleTapOnLabel)) self.isUserInteractionEnabled = true self.addGestureRecognizer(tap) } override var attributedText: NSAttributedString?{ didSet{ if let _attributedText = attributedText{ self.textStorage = NSTextStorage(attributedString: _attributedText) self.layoutManager.addTextContainer(self.textContainer) self.textStorage?.addLayoutManager(self.layoutManager) self.textContainer.lineFragmentPadding = 0.0; self.textContainer.lineBreakMode = self.lineBreakMode; self.textContainer.maximumNumberOfLines = self.numberOfLines; } } } func handleTapOnLabel(tapGesture:UITapGestureRecognizer){ let locationOfTouchInLabel = tapGesture.location(in: tapGesture.view) let labelSize = tapGesture.view?.bounds.size let textBoundingBox = self.layoutManager.usedRect(for: self.textContainer) let textContainerOffset = CGPoint(x: ((labelSize?.width)! - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, y: ((labelSize?.height)! - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y) let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x, y: locationOfTouchInLabel.y - textContainerOffset.y) let indexOfCharacter = self.layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) self.attributedText?.enumerateAttribute(NSLinkAttributeName, in: NSMakeRange(0, (self.attributedText?.length)!), options: NSAttributedString.EnumerationOptions(rawValue: UInt(0)), using:{ (attrs: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in if NSLocationInRange(indexOfCharacter, range){ if let _attrs = attrs{ UIApplication.shared.openURL(URL(string: _attrs as! String)!) } } }) }} 

This class was created by reusing the code from this. To attribute strings, execute this answer . And here you can find how to make phone URLs.

+10
Nov 22 '16 at 16:35
source share

I really liked to use this, because it creates a link with blue color for a specific text, but not for the entire text of the label: FRHyperLabel

Clever use of the hyperlink on the Terms of Use

To do:

  • Download from the above link and copy FRHyperLabel.h , FRHyperLabel.m into your project.

  • Drag the UILabel the Storyboard and define the name of the custom class FRHyperLabel in the inspector ID, as shown in the figure.

enter image description here

  1. Connect your UILabel from the storyboard to the viewController.h file

@property (weak, nonatomic) IBOutlet FRHyperLabel *label;

  1. Now in your viewController.m file add the following code.

`NSString * string = @" By downloading, I agree to the Terms of Use "; NSDictionary * attributes = @ {NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleHeadline]};

 _label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes]; [_label setFont:[_label.font fontWithSize:13.0]]; [_label setLinkForSubstring:@"Terms of Use" withLinkHandler:^(FRHyperLabel *label, NSString *substring){ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]]; }];` 
  1. And run it.
+6
Mar 04 '16 at 12:11
source share

Use a UITextView instead of a UILabel, and it has the property to convert text to hyperlink

Quick code:

 yourTextView.editable = false yourTextView.dataDetectorTypes = UIDataDetectorTypes.All //or yourTextView.dataDetectorTypes = UIDataDetectorTypes.PhoneNumber //or yourTextView.dataDetectorTypes = UIDataDetectorTypes.Link 
+6
Jul 04 '16 at 16:54
source share
 extension UITapGestureRecognizer { func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool { let layoutManager = NSLayoutManager() let textContainer = NSTextContainer(size: CGSize.zero) let textStorage = NSTextStorage(attributedString: label.attributedText!) // Configure layoutManager and textStorage layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) // Configure textContainer textContainer.lineFragmentPadding = 0.0 textContainer.lineBreakMode = label.lineBreakMode textContainer.maximumNumberOfLines = label.numberOfLines textContainer.size = label.bounds.size // main code let locationOfTouchInLabel = self.location(in: label) let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInLabel, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) let indexOfCharacterRange = NSRange(location: indexOfCharacter, length: 1) let indexOfCharacterRect = layoutManager.boundingRect(forGlyphRange: indexOfCharacterRange, in: textContainer) let deltaOffsetCharacter = indexOfCharacterRect.origin.x + indexOfCharacterRect.size.width if locationOfTouchInLabel.x > deltaOffsetCharacter { return false } else { return NSLocationInRange(indexOfCharacter, targetRange) } } } 
0
May 31 '17 at 11:56
source share



All Articles