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

Rob*_*Rob 111 iphone url objective-c uilabel

我想在我的应用上创建一个可点击的标签,引导我访问Safari网页.我还希望用户只能通过点击来拨打电话号码?

谢谢你的建议

Bas*_*sel 128

您可以使用a UITextView并在检查器中选择检测链接,电话号码和其他内容.

  • 我有一个可以容纳 N 行文本的自动调整大小标签。如何在不实现 UITextView 的情况下使此标签中的链接可点击。 (2认同)

Sam*_*nga 93

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

Objective-C的:

yourTextView.editable = NO;
yourTextView.dataDetectorTypes = UIDataDetectorTypeAll;
Run Code Online (Sandbox Code Playgroud)

迅速:

yourTextView.editable = false; 
yourTextView.dataDetectorTypes = UIDataDetectorTypes.All;
Run Code Online (Sandbox Code Playgroud)

这将自动检测链接.

有关详细信息,请参阅文档

  • 这会将链接变为蓝色并将其加下划线,但似乎确实可以点击它,至少在iOS 7上是这样. (5认同)
  • 问题是关于UILabel,而不是UITextView. (4认同)
  • Swift 代码: yourTextView.editable = false; yourTextView.dataDetectorTypes = UIDataDetectorTypes.All; (2认同)

Ram*_*ury 28

https://github.com/mattt/TTTAttributedLabel

这绝对是你需要的.您还可以为标签应用属性,例如下划线,并为其应用不同的颜色.只需查看可点击网址的说明即可.

主要是,您执行以下操作:

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


Roh*_*wan 12

你可以自定义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

}
Run Code Online (Sandbox Code Playgroud)


Boj*_*vic 10

如果你想让它由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)!)
            }
        }
    })

}}
Run Code Online (Sandbox Code Playgroud)

这个课程是通过重用这个答案中的代码来完成的.为了制作属性字符串,请查看此答案.而在这里,你可以找到如何让手机网址.


kar*_*ran 6

使用这个我非常喜欢它,因为创建了蓝色到特定文本的链接,而不是整个标签文本:FRHyperLabel

巧妙地在使用条款上应用超链接

去做:

  1. 从上面的链接下载并复制FRHyperLabel.h,FRHyperLabel.m您的项目.

  2. 拖放UILabel您的Storyboard并定义自定义类名称以FRHyperLabel识别检查器,如图所示.

在此输入图像描述

  1. 将故事板中的UILabel连接到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"]];
}];`
Run Code Online (Sandbox Code Playgroud)
  1. 并运行它.


Thi*_*guy 6

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

SWIFT代码:

yourTextView.editable = false
yourTextView.dataDetectorTypes = UIDataDetectorTypes.All
//or
yourTextView.dataDetectorTypes = UIDataDetectorTypes.PhoneNumber
//or
yourTextView.dataDetectorTypes = UIDataDetectorTypes.Link
Run Code Online (Sandbox Code Playgroud)

  • 如何在UILabel中使用的问题,而不是TextViews.谢谢. (4认同)