Xcode 创建可点击的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12916569/
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-09-15 01:50:28  来源:igfitidea点击:

Xcode create clickable text

xcodetexthyperlinkclickable

提问by Jigar P

I am trying to create an object that can have clickable text within it.

我正在尝试创建一个可以在其中包含可点击文本的对象。

For instance: I have text displayed to the user: "Please verify the terms and conditions prior to accepting registration."

例如:我向用户显示了文本: “请在接受注册之前验证条款和条件。”

I want to make just the "terms and conditions"text show as a link. The text would be underlined an blue.

我只想将“条款和条件”文本显示为链接。文本将带有蓝色下划线。

When the user clicks the text I want them to be navigated to my terms and conditions viewcontroller page. So the link is internal to the application, not an external web page.

当用户单击文本时,我希望它们被导航到我的条款和条件视图控制器页面。所以链接是应用程序内部的,而不是外部网页。

Questions

问题

  1. How do I display the text with only making the specific text linkable?
  2. How do I 'seque' to the ViewController page via the user clicking the linked text?
  1. 如何仅使特定文本可链接来显示文本?
  2. 如何通过用户单击链接文本“序列”到 ViewController 页面?

采纳答案by Isaac

You can do this with TTTAttributedLabel. From the readme file:

你可以用TTTAttributedLabel做到这一点。从自述文件:

In addition to supporting rich text, TTTAttributedLabel allows you to automatically detect links for URLs, addresses, phone numbers, and dates, or allow you to embed your own.

label.dataDetectorTypes = UIDataDetectorTypeAll; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)

label.text = @"Fork me on GitHub! (http://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]; // Embedding a custom link in a substring

除了支持富文本之外,TTTAttributedLabel 还允许您自动检测 URL、地址、电话号码和日期的链接,或者允许您嵌入自己的链接。

label.dataDetectorTypes = UIDataDetectorTypeAll; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)

label.text = @"Fork me on GitHub! (http://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]; // Embedding a custom link in a substring