ios 如何使用 Swift 创建“超链接”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25477547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:02:00  来源:igfitidea点击:

How can I create a "hyperlink" with Swift?

ioscocoa-touchhyperlinkswift

提问by AstroCB

I'm trying to make separate pieces of text UILabels clickable. What I'm looking for is commonly known as a hyperlink in web development.

我正在尝试使单独的文本片段UILabel可点击。我正在寻找的东西通常被称为 Web 开发中的超链接。

<a href="//example.com">Link 1</a>
<a href="//example.com/example">Link 2</a>
<a href="//example.com/other_example">Link 3</a>

Each atag is its own UILabel, and it would ideally open Safari to the specified hrefwhen the text between the tags is clicked.

每个a标签都是它自己的UILabel,理想情况下,href当点击标签之间的文本时,它会打开 Safari 到指定的位置。

I've found a bevy of resources on how to do this sort of thing in Objective-C, but they all seem unnecessarily complicated and don't translate well to Swift (they fit an Objective-C organizational structure that doesn't work well in Swift and goes against the recommended way of using the language).

我找到了很多关于如何在 Objective-C 中做这种事情的资源,但它们似乎都不必要地复杂,并且不能很好地转换为 Swift(它们适合无法很好地工作的 Objective-C 组织结构在 Swift 中,并且违背了使用该语言的推荐方式)。

Here are a few:

以下是一些:

If I had a 3 UILabels,

如果我有一个3UILabel秒,

Item 1

Item 2

Item 3

第 1 项

第 2 项

第 3 项

then what would be the best "Swift-y" way to make each item open to a different URL in Safari?

那么让每个项目在 Safari 中打开到不同 URL 的最佳“Swift-y”方式是什么?

I could create separate buttons for each, but the UILabels are programmatically populated, so I was thinking that making the text respond to taps might be a better option.

我可以为每个按钮创建单独的按钮,但UILabels 是以编程方式填充的,所以我认为让文本响应点击可能是更好的选择。

回答by Jorge Cordero

Swift 3I created a LinkUILabel class in github: https://github.com/jorgecsan/LinkUILabelWith this you only need add the url inspectable as the shows the image: Example label with urlor assign the url variable programmatically:

Swift 3我在 github 中创建了一个 LinkUILabel 类:https: //github.com/jorgecsan/LinkUILabel有了这个,您只需要添加可检查的 url,如图所示: 带有 url 的示例标签或以编程方式分配 url 变量:

linkUILabel.url = "www.example.com"

If you want to implement by your self also I found that solution!:)

如果您想自己实施,我也找到了解决方案!:)

using:

使用:

// This is the label
@IBOutlet weak var label: UILabel!

override func loadView() {
    super.loadView()

    // This is the key
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClicLabel(sender:)))
    label.isUserInteractionEnabled = true
    label.addGestureRecognizer(tap)
}

// And that's the function :)
func onClicLabel(sender:UITapGestureRecognizer) {
    openUrl("http://www.google.com")
}


func openUrl(urlString:String!) {
    let url = URL(string: urlString)!
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}

Hope it helps!:)

希望能帮助到你!:)

回答by Paulw11

The One approach would be something like the following.
The assumptions are:

One 方法类似于以下内容。
假设是:

  • self.urls is a string array containing the urls associated with each UILabel.
  • Each UILabeltaghas been set to the corresponding index in the array
  • labelTapped:is set as the touchUpInsidehandler for the labels.
  • self.urls 是一个字符串数组,包含与每个UILabel.
  • 每个UILabeltag都被设置为数组中的对应索引
  • labelTapped:被设置为touchUpInside标签的处理程序。
import Foundation
import UIKit

class urltest {

    var urls:[String]

    init() {
        self.urls=[String]()  // Load URLs into here
    }

    @IBAction func labelTapped(sender:UILabel!) {

        let urlIndex=sender.tag;
        if (urlIndex >= 0 && urlIndex < self.urls.count) {
           self.openUrl(self.urls[urlIndex]);
        }

    }

    func openUrl(url:String!) {

        let targetURL=NSURL.URLWithString(url)

        let application=UIApplication.sharedApplication()

        application.openURL(targetURL);

    }
}