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

发布于 2024-12-26 16:02:49 字数 656 浏览 1 评论 0 原文

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

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

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

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

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

I have a UILabel whose 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.

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

This is the complete text for UILabel. I have only one UILabel for 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 :

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白衬杉格子梦 2025-01-02 16:02:49

您可以将自定义操作添加到任何可用的 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];

然后,在您的 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 */
    }
}

TTTAttributedLabel 是OH 属性标签。

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

You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you'll intercept later:

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:

- (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 */
    }
}

TTTAttributedLabel is a fork of OHAttributedLabel.

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

三人与歌 2025-01-02 16:02:49

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

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

鹿! 2025-01-02 16:02:49

我的项目已成功使用 OHAttributedLabel 来实现此目的。查看

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

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

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

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

-(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 NO in this case (to keep the default action from happening too), you could just do whatever you wanted like navigation, etc.

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.

玩世 2025-01-02 16:02:49

有一个名为 FancyLabel 的项目正是您所需要的。但它可能需要一些定制。
另外,我认为 Three20 具有此功能,但如果您尚未使用,它可能有点过大了它。

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

您还可以通过覆盖 openURL 来自定义操作来响应这些链接上的点击事件,如 此处

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

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

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)

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

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.

难得心□动 2025-01-02 16:02:49

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

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];

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];
情释 2025-01-02 16:02:49

我不喜欢被迫使用 UITextView 或第三方库,因为我需要的只是一个渲染链接的轻量级标签(并且在它们被点击时告诉我!)

这是我的尝试在能够检测链接点击的轻量级 UILabel 子类中。该方法与我见过的其他方法不同,它通过添加到 NSTextStorage 的委托回调来访问 UILabel 的共享 NSLayoutManager通过类别扩展。美妙之处在于 UILabel 执行其本机布局和绘图 - 其他 UILabel 替代品通常会使用额外的 NSLayoutManager/NSTextContainer

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

https://github.com/TomSwift/TSLabel

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

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

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

https://github.com/TomSwift/TSLabel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文