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
How can I create a "hyperlink" with Swift?
提问by AstroCB
I'm trying to make separate pieces of text UILabel
s 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 a
tag is its own UILabel
, and it would ideally open Safari to the specified href
when 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:
以下是一些:
- How to add hyperlink in iPhone app?
- How to make a clickable link inside a NSTextField and Cocoa
- Text as Hyperlink in Objective-C
If I had a 3 UILabel
s,
如果我有一个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 UILabel
s are programmatically populated, so I was thinking that making the text respond to taps might be a better option.
我可以为每个按钮创建单独的按钮,但UILabel
s 是以编程方式填充的,所以我认为让文本响应点击可能是更好的选择。
回答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:
or assign the url variable programmatically:
Swift 3我在 github 中创建了一个 LinkUILabel 类:https:
//github.com/jorgecsan/LinkUILabel有了这个,您只需要添加可检查的 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
UILabel
tag
has been set to the corresponding index in the array labelTapped:
is set as thetouchUpInside
handler for the labels.
- self.urls 是一个字符串数组,包含与每个
UILabel
. - 每个
UILabel
tag
都被设置为数组中的对应索引 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);
}
}