如何使URL /电话可点击的UILabel?

我想在我的应用程序上创build一个可点击的标签,并将其引导至Safari网页。 我也希望用户能够通过点击拨打电话号码?

感谢您的build议

您可以使用UITextView ,然后在检查器中select检测链接,电话号码和其他内容。

使用UITextView而不是UILabel ,它有一个属性来将您的文本转换为超链接

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

请按照文档更多的细节。 这将自动检测链接。

希望这会帮助你。 谢谢 :)

https://github.com/mattt/TTTAttributedLabel

这绝对是你需要的。 您也可以为您的标签应用属性,如下划线,并为其应用不同的颜色。 只需检查可点击的url的说明。

主要是,你做类似下面的事情:

 NSRange range = [label.text rangeOfString:@"me"]; [label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring 

你可以使自定义的UIButton和setText是你想要的,并添加方法。

  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 } 

使用这个我很喜欢它,因为创build与特定文本的蓝色链接而不是整个标签文本: FRHyperLabel

根据使用条款巧妙应用超链接

去做:

  1. 从上面下载链接并将FRHyperLabel.hFRHyperLabel.m复制到您的项目中。

  2. 拖放您的Storyboard UILabel ,并将自定义类名称定义到FRHyperLabel中,如图所示。

在这里输入图像说明

  1. 将你的UILabel从storyboard连接到你的viewController.h文件

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

  1. 现在在你的viewController.m文件中添加下面的代码。

`NSString * string = @“通过上传,我同意使用条款”; 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. 并运行它。

如果你想这个由UILabel而不是UITextView来处理,你可以使UILabel的子类如下所示:

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

这个类是通过重用这个答案的代码。 为了使属性的string检查出这个答案 。 在这里你可以find如何使手机url。

使用UITextView而不是UILabel,它有一个属性来将您的文本转换为超链接

SWIFT代码:

 yourTextView.editable = false yourTextView.dataDetectorTypes = UIDataDetectorTypes.All //or yourTextView.dataDetectorTypes = UIDataDetectorTypes.PhoneNumber //or yourTextView.dataDetectorTypes = UIDataDetectorTypes.Link 
 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) } } }