ios Swift 中标签内的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44365890/
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
Link within label in Swift
提问by KevinB
I would like to do that in my app :
我想在我的应用程序中做到这一点:
The label is like : usernamecomment
标签是这样的:用户名评论
I don't know how to add the "button" within the label; I found this library but I'm not sure it will work? https://github.com/optonaut/ActiveLabel.swift
我不知道如何在标签中添加“按钮”;我找到了这个库,但我不确定它会起作用吗?https://github.com/optonaut/ActiveLabel.swift
Maybe use it by creating a regex for the first word? What do you think?
也许通过为第一个单词创建正则表达式来使用它?你怎么认为?
回答by Ahmad F
For such a case, instead of adding it as a UILabel
component, I would rather use UITextView
, because it has dataDetectorTypesproperty:
对于这种情况,UILabel
我宁愿使用UITextView
,而不是将其添加为组件,因为它具有dataDetectorTypes属性:
The types of data converted to tappable URLs in the text view.
You can use this property to specify the types of data (phone numbers, http links, and so on) that should be automatically converted to URLs in the text view. When tapped, the text view opens the application responsible for handling the URL type and passes it the URL. Note thatdata detection does not occur if the text view's isEditable propertyis set to true.
在文本视图中转换为可点击 URL 的数据类型。
您可以使用此属性指定应在文本视图中自动转换为 URL的数据类型(电话号码、 http 链接等)。当点击时,文本视图打开负责处理 URL 类型的应用程序并将 URL 传递给它。请注意,如果文本视图的 isEditable 属性设置为 true ,则不会发生数据检测。
So, you can implement it as:
因此,您可以将其实现为:
// as mentioned in the documentation, make sure to let it be uneditable:
textView.isEditable = false
textView.dataDetectorTypes = .link
For your case, I assume that it shall be .link
. I would also suggest to check the other options for the UIDataDetectorTypes.
对于你的情况,我认为它应该是.link
. 我还建议检查UIDataDetectorTypes的其他选项。
回答by Hyman
Use Active-Label https://github.com/optonaut/ActiveLabel.swiftMake a UILabel
named label
使用 Active-Label https://github.com/optonaut/ActiveLabel.swift 创建一个UILabel
命名label
let customType = ActiveType.custom(pattern: "\sjohncena\b") //Looks for "supports"
label.enabledTypes.append(customType3)
label.handleCustomTap(for: customType) { self.alert("Custom type", message: label.text = @"Fork me on GitHub! (https://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range];
) }
func alert(_ title: String, message: String) {
let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
present(vc, animated: true, completion: nil)
}
where johncena
is string which is clickable.
see https://github.com/optonaut/ActiveLabel.swift/blob/master/ActiveLabelDemo/ViewController.swift
哪里johncena
是可点击的字符串。见https://github.com/optonaut/ActiveLabel.swift/blob/master/ActiveLabelDemo/ViewController.swift
回答by Aravind A R
You can use TTTAttributedLabelto make a particular part or word tappable in a UILabel.
您可以使用TTTAttributedLabel使 UILabel 中的特定部分或单词可点击。
For example
例如
(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
NSLog(@"link %@", [url absoluteString]);
NSLog(@"whole label %@", label);
}
In addition to hyperlinks you can add custom links also to a particular part or word and that word becomes tappable and on tapping that word delegate method didSelectLinkWithURL gets called and you can check for that link inside that function
除了超链接之外,您还可以将自定义链接添加到特定部分或单词,并且该单词变得可点击,点击该单词时会调用 didSelectLinkWithURL 方法,您可以在该函数中检查该链接
##代码##