English 中文(简体)
UILabel -string as text and links
原标题:UILabel - string as text and links

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 Telephone number is 645-345-2345 ,我的地址是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.

最佳回答

您可在的任何现有代号上添加习俗行动,支持使用fake/em>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];

随后,在<条码>中,TTTAttributed LaborelDelegate:

- (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是公共卫生和社会福利部的一个论坛。

如果你想采取更为复杂的办法,请上。 它支持海关连接外箱。

问题回答

You can use UITextView with Phone numbers and links detection YES, scrolling disabled YES user interaction enabled YES, instead of 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.

还有一个更为简单的解决办法,如果你的所有联系都是电话地址。 你们只能使用UITextView,而不是“IDLabel”。 它自动探测电话、地址等。 (正确检查IB中的箱子)

如以下解释,你也可以采取习俗行动,以点击有关这些联系的事件: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.

你们可以利用习俗纽扣来看待联系。 如果你打算使用纽芬兰语,你也可以在习俗标签上添加姿态。

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

当我需要一个轻重标签,使连接起来时,我不会被迫使用<条码>UITextView或第三方的校正(当我重新开发时告诉我!)

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





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...