xcode 在 UILabel 中插入超链接

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

Insert a hyperlink in UILabel

objective-cxcodehyperlinknsstringuilabel

提问by Ackman

i have a response coming in from the api in form of a text I want to scan that text for the word "Planned Parenthood health center" and insert a hyperlink on that word which redirects to plannedparenthood.org/health-center portal.

我有来自 api 的文本形式的响应我想扫描该文本以查找“Planned Parenthood health center”一词,并在该词上插入一个超链接,该超链接重定向到plannedparenthood.org/health-center 门户。

my approach:

我的方法:

NSString *text = response.content;
text = [text stringByReplacingOccurrencesOfString:@"Planned Parenthood health center" 
                                       withString:@"<a href=\"http://www.plannedparenthood.org/health-center\">Planned Parenthood health center</a>"];

though the link on the text is getting replaced. It is getting replaced by

虽然文本上的链接正在被替换。它正在被取代

<a href=\"http://www.plannedparenthood.org/health-center\">Planned Parenthood health center </a>

What's wrong why am I not getting any links there? am I doing something wrong I have also set the user enabled interaction to YES

怎么了,为什么我在那里没有得到任何链接?我做错了什么我还将用户启用的交互设置为是

回答by Josh Gafni

iOS does not handle this well to be honest probably because it intends you to use UIButtons which are much more easily tappable for users.

老实说,iOS 不能很好地处理这个问题,这可能是因为它打算让您使用 UIButtons,这些 UIButtons 对用户来说更容易点击。

To accomplish what you want though, you cannot use a UILabel - you need to use a UITextView and set its attributedTextproperty:

但是,要完成您想要的操作,您不能使用 UILabel - 您需要使用 UITextView 并设置其attributedText属性:

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"Planned Parenthood health center"];
[attributedString addAttribute: NSLinkAttributeName value: @"http://www.plannedparenthood.com" range: NSMakeRange(0, str.length)];
textView.attributedText = attributedString;

Note that the UITextView must be selectable but not editable.

请注意, UITextView 必须是可选择的但不可编辑的。

There is a bit of a downside with this method if you have other text in your attributed string that isn't linked. For example, if you have "Please select herein order to go to Planned Parenthood's website" where "here" is the only part of the text that you want linked. In this case, if you tap anywhere else in the text beside the word "here", you'll notice that it will be able to be selected by the user and have the blue highlight.

如果您的属性字符串中有其他未链接的文本,则此方法有一些缺点。例如,如果您有“请选择此处以访问 Planned Parenthood 的网站”,其中“此处”是您想要链接的文本的唯一部分。在这种情况下,如果您点击“此处”一词旁边的文本中的任何其他位置,您会注意到用户可以选择它并具有蓝色突出显示。

If using a UITextView is not desired, then you will need to use something custom such as TTTAttributedLabel

如果不希望使用 UITextView,那么您将需要使用诸如TTTAttributedLabel 之类的自定义内容