Xcode创建可点击的文本

时间:2012-10-16 14:09:59

标签: xcode text hyperlink clickable

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

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

我想将“条款和条件”文字显示为链接。该文本将加下划线蓝色。

当用户点击文本时,我希望将它们导航到我的条款和条件viewcontroller页面。因此,链接是应用程序的内部链接,而不是外部网页。

问题

  1. 如何仅使特定文本可链接来显示文本?
  2. 如何通过用户点击链接文本来“查看”ViewController页面?

1 个答案:

答案 0 :(得分:4)

您可以使用TTTAttributedLabel执行此操作。来自自述文件:

  

除支持富文本外,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
相关问题