如何在NSAttributedString中创建可单击的链接? [英] How can I make a clickable link in an NSAttributedString?

查看:114
本文介绍了如何在NSAttributedString中创建可单击的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITextView 中点击超链接是微不足道的。您只需在IB中的视图上设置检测链接复选框,它就会检测HTTP链接并将其转换为超链接。

It's trivial to make hyperlinks clickable in a UITextView. You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks.

然而,这仍然意味着用户看到的是原始链接。 RTF文件和HTML都允许您设置用户可读的字符串,其中包含后面链接。

However, that still means that what the user sees is the "raw" link. RTF files and HTML both allow you to set up a user-readable string with a link "behind" it.

很容易将属性文本安装到文本视图中(或 UILabel UITextField ,就此而言。)但是,当该属性文本包含链接时,它不可点击。

It's easy to install attributed text into a text view (or a UILabel or UITextField, for that matter.) However, when that attributed text includes a link, it is not clickable.

有没有办法让用户可读文本可在 UITextView UILabel UITextField 中点击?

Is there a way to make user-readable text clickable in a UITextView, UILabel or UITextField?

标记在SO上有所不同,但这是一般的想法。我想要的是这样的文字:

The markup is different on SO, but here is the general idea. What I want is text like this:


这个变形是用 Face Dancer ,点击查看应用程序商店。

This morph was generated with Face Dancer, Click to view in the app store.

我唯一可以get是这样的:

The only thing I can get is this:


这个变形是用Face Dancer生成的,点击 http://example.com/facedancer 可在应用商店中查看。

This morph was generated with Face Dancer, Click on http://example.com/facedancer to view in the app store.


推荐答案

我的问题的核心是我希望能够在文本视图/字段/标签中创建可点击链接,而无需编写自定义代码来操作文本和添加链接。我希望它是数据驱动的。

The heart of my question was that I wanted to be able to create clickable links in text views/fields/labels without having to write custom code to manipulate the text and add the links. I wanted it to be data-driven.

我终于想出了如何做到这一点。问题是IB不尊重嵌入式链接。

I finally figured out how to do it. The issue is that IB doesn't honor embedded links.

此外,iOS版本的 NSAttributedString 不允许您从RTF文件初始化属性字符串。 OS X版本的 NSAttributedString 确实有一个初始化程序,它将RTF文件作为输入。

Furthermore, the iOS version of NSAttributedString doesn't let you initialize an attributed string from an RTF file. The OS X version of NSAttributedString does have an initializer that takes an RTF file as input.

NSAttributedString 符合NSCoding协议,因此您可以将其转换为NSData或从NSData转换

NSAttributedString conforms to the NSCoding protocol, so you can convert it to/from NSData

我创建了一个OS X命令行工具,它将RTF文件作为输入,并输出扩展名为.data的文件,该文件包含NSCoding中的NSData。然后我将.data文件放入我的项目中,并添加几行代码,将文本加载到视图中。代码看起来像这样(这个项目在Swift中):

I created an OS X command line tool that takes an RTF file as input and outputs a file with the extension .data that contains the NSData from NSCoding. I then put the .data file into my project and add a couple of lines of code that loads the text into the view. The code looks like this (this project was in Swift) :

/*
If we can load a file called "Dates.data" from the bundle and convert it to an attributed string,
install it in the dates field. The contents contain clickable links with custom URLS to select
each date.
*/
if
  let datesPath = NSBundle.mainBundle().pathForResource("Dates", ofType: "data"),
  let datesString = NSKeyedUnarchiver.unarchiveObjectWithFile(datesPath) as? NSAttributedString
{
  datesField.attributedText = datesString
}

For使用大量格式化文本的应用程序,我创建一个构建规则,告诉Xcode给定文件夹中的所有.rtf文件都是源文件,.data文件是输出。一旦我这样做,我只需将.rtf文件添加到指定目录(或编辑现有文件),构建过程就会发现它们是新的/更新的,运行命令行工具,并将文件复制到应用程序包中。它工作得很漂亮。

For apps that use a lot of formatted text, I create a build rule that tells Xcode that all the .rtf files in a given folder are source and the .data files are the output. Once I do that, I simply add .rtf files to the designated directory, (or edit existing files) and the build process figures out that they are new/updated, runs the command line tool, and copies the files into the app bundle. It works beautifully.

我写了一篇博客文章,链接到展示该技术的示例(Swift)项目。你可以在这里看到它:

I wrote a blog post that links to a sample (Swift) project demonstrating the technique. You can see it here:

在您的应用中打开的UITextField中创建可点击的网址

这篇关于如何在NSAttributedString中创建可单击的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆