How can I make a clickable link in an NSAttributedString?(如何在 NSAttributedString 中创建可点击链接?)
问题描述
在 UITextView
中使超链接可点击是微不足道的.您只需在 IB 的视图上设置检测链接"复选框,它就会检测 HTTP 链接并将它们转换为超链接.
但是,这仍然意味着用户看到的是原始"链接.RTF 文件和 HTML 都允许您设置用户可读的字符串,并在其后面"添加一个链接.
很容易将属性文本安装到文本视图(或 UILabel
或 UITextField
,就此而言.)但是,当属性文本包含链接时,它不可点击.
有没有办法让 UITextView
、UILabel
或 UITextField
中的用户可读文本可点击?
SO 上的标记有所不同,但这是大致的想法.我想要的是这样的文字:
<块引用>这个变形是用
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.
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.
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.
Is there a way to make user-readable text clickable in a UITextView
, UILabel
or UITextField
?
The markup is different on SO, but here is the general idea. What I want is text like this:
This morph was generated with Face Dancer, Click to view in the app store.
The only thing I can get is this:
This morph was generated with Face Dancer, Click on http://example.com/facedancer to view in the app store.
I found this really useful but I needed to do it in quite a few places so I've wrapped my approach up in a simple extension to NSMutableAttributedString
:
Swift 3
Swift 2
Example usage:
Objective-C
I've just hit a requirement to do the same in a pure Objective-C project, so here's the Objective-C category.
Example usage:
Make Sure that the NSTextField's Behavior attribute is set as Selectable.
这篇关于如何在 NSAttributedString 中创建可点击链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!