ios UILabel - 作为文本和链接的字符串

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

UILabel - string as text and links

iphoneiosnsstringuilabel

提问by Nitish

I have a UILabelwhose text I am getting from a server. Some of the text is to be identified as links, and on touching those links some action should be performed. e.g.

我有一个UILabel我从服务器获取的文本。一些文本将被标识为链接,并且在触摸这些链接时应该执行一些操作。例如

NSString *str = @"My phone number is 645-345-2345and my address is xyz";

NSString *str = @"我的电话号码是645-345-2345,我的地址是xyz";

This is the complete text for UILabel. I have only one UILabelfor displaying this text (Text is dynamic. I just gave an example.). On clicking these links I need to perform actions like navigating to some different screen or make a call.
I know that I can display such text with help of OHAttributedLabel. And the links can be displayed as follows :

这是 的完整文本UILabel。我只有一个UILabel用于显示此文本(文本是动态的。我只是举了一个例子。)。单击这些链接时,我需要执行一些操作,例如导航到某个不同的屏幕或拨打电话。
我知道我可以在OHAttributedLabel 的帮助下显示这样的文本。链接可以显示如下:

[label1 addCustomLink:[NSURL URLWithString:@"http://www.foodreporter.net"] inRange:[txt rangeOfString:someString]];  

But I wonder how can I make these text linksperform some action like navigation to different screen or making a call.
Let me know if more explanation is required.

但我想知道如何让这些文本链接执行一些操作,例如导航到不同的屏幕或拨打电话。
如果需要更多解释,请告诉我。

回答by djromero

You can add custom actions to any of the available UILabelreplacements that support links using a fakeURL schemethat you'll intercept later:

您可以将自定义操作添加到任何UILabel支持使用虚假URL 方案的链接的可用替换项中,稍后您将截获该方案

TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"]; 
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r];

Then, in your TTTAttributedLabelDelegate:

然后,在您的TTTAttributedLabelDelegate

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([[url scheme] hasPrefix:@"action"]) {
        if ([[url host] hasPrefix:@"show-help"]) {
            /* load help screen */
        } else if ([[url host] hasPrefix:@"show-settings"]) {
            /* load settings screen */
        }
    } else {
        /* deal with http links here */
    }
}

TTTAttributedLabelis a fork of OHAttributedLabel.

TTTAttributedLabelOHAttributedLabel的一个分支。

If you want a more complex approach, have a look to Nimbus Attributed Label. It support custom links out-of-the-box.

如果您想要更复杂的方法,请查看Nimbus Attributed Label。它支持开箱即用的自定义链接。

回答by Ganesh

You can use UITextView with Phone numbers and links detection YES, scrolling disabled YES user interaction enabled YES, instead of UILabel.

您可以将 UITextView 与电话号码和链接检测 YES、滚动禁用 YES 用户交互启用 YES 一起使用,而不是 UILabel。

回答by Martin Gjaldbaek

My project has successfully used OHAttributedLabel for this. Check out the

我的项目为此成功使用了 OHAttributedLabel。查看

-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;

-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;

method in OHAttributedLabelDelegate(link). It allows you to decide what happens when a link is clicked. If you look at the source for the example from the OHAttributedLabel project, it's used to display an alert. If you returned NOin this case (to keep the default action from happening too), you could just do whatever you wanted like navigation, etc.

OHAttributedLabelDelegate链接)中的方法。它允许您决定单击链接时会发生什么。如果您查看OHAttributedLabel 项目中示例的源代码,它用于显示警报。如果你NO在这种情况下返回(为了防止默认操作也发生),你可以做任何你想做的事情,比如导航等。

Note however that this requires that you can determine the action correctly just from the text. For our project, we used a slightly fancier solution, where the server sent us text with tags in them and a list of commands to perform for each tag.

但是请注意,这要求您可以仅从文本中正确确定操作。对于我们的项目,我们使用了一个稍微高级的解决方案,其中服务器向我们发送带有标签的文本以及要为每个标签执行的命令列表。

回答by adamsiton

There a project called FancyLabelthat is about what you need. It might need some customization though. Also, I think Three20has this functionality, but it might be an overkill if you don't already use it.

有一个名为FancyLabel的项目是关于您所需要的。不过,它可能需要一些自定义。另外,我认为Three20有这个功能,但如果你还没有使用它,它可能是一种矫枉过正。

There's also a much simpler solution, if all of your links are phones \ addresses \ urls. You can simply use a UITextView instead of a UILabel. It has auto detection of phones, address, etc. (just check the boxes in IB)

还有一个更简单的解决方案,如果您的所有链接都是电话\地址\网址。您可以简单地使用 UITextView 而不是 UILabel。它具有自动检测电话、地址等功能(只需选中 IB 中的框)

You can also have custom actions in response to click events on those links by overriding openURL, as explained here

您也可以响应的自定义操作通过重写的OpenURL点击这些链接事件,说明这里

Is there a specific reason that you must use a UILabel instead of a UITextView? Note that a lot of the implementations of attributed labels inherit from UIView or don't implement all of UILabel's functionality.

是否有特定原因必须使用 UILabel 而不是 UITextView?请注意,很多属性标签的实现都是从 UIView 继承的,或者没有实现 UILabel 的所有功能。

回答by Ali3n

You can use custom button to give a look like of link ..Also you can add gesture on the custom label if you dont want to use button ..

您可以使用自定义按钮来提供链接的外观..此外,如果您不想使用按钮 .. 也可以在自定义标签上添加手势。

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];

回答by TomSwift

I'm not a fan of being forced to use UITextViewor a third party lib when all I need is a lightweight label that renders links (and that tells me when they're tapped!)

UITextView当我只需要一个呈现链接的轻量级标签时,我不喜欢被迫使用或第三方库(这会告诉我它们何时被点击!)

Here's my attempt at a lightweight UILabelsubclass able to detect link taps. The approach is different from others I've seen in that it gains access to the UILabel's shared NSLayoutManagervia a delegate callback added to NSTextStoragevia a category extension. The beauty is that UILabelperforms its native layout and drawing - other UILabelreplacements often augment or replace the native behavior with an additional NSLayoutManager/NSTextContainer.

这是我对UILabel能够检测链接点击的轻量级子类的尝试。该方法与我见过的其他方法不同,因为它可以通过通过类别扩展添加到的委托回调来访问UILabel共享的。美妙之处在于执行其本机布局和绘图 - 其他替换通常用额外的/来增强或替换本机行为。 NSLayoutManagerNSTextStorageUILabelUILabelNSLayoutManagerNSTextContainer

Probably App Store safe, but somewhat fragile - use at your own risk!

可能 App Store 安全,但有些脆弱 - 使用风险自负!

https://github.com/TomSwift/TSLabel

https://github.com/TomSwift/TSLabel