MZSelectableLabel

A simple to use drop in replacement for UILabel for iOS 7 that provides automatic detection of colors.

How To Use

  1. Add the files MZSelectableLabel.m and MZSelectableLabel.h to your project.
  2. Design your user interface as you would normally. In Interface Builder set the custom class for any UILabel you want to replace to MZSelectableLabel. The label should honour all IB settings or create MZSelectableLabel objects in code.

Additional:

You can set automatic foreground color detection:

@property (nonatomic, assign, getter = isAutomaticForegroundColorDetectionEnabled) IBInspectable BOOL automaticForegroundColorDetectionEnabled;
@property (nonatomic, strong) IBInspectable UIColor *skipColorForAutomaticDetection;

This means that if you have attributed string, MZSelectableLabel will detect all ranges for you. Even you can skip detection for default color for example [UIColor blackColor].

Example

[self.label setSelectableRange:[[self.label.attributedText string] rangeOfString:@"Tap me"] hightlightedBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.3]];

self.label.selectionHandler = ^(NSRange range, NSString *string) {

    NSString *message = [NSString stringWithFormat:@"You tapped %@", string];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"Dismiss"
                                          otherButtonTitles:nil];
    [alert show];
};

Demo

Repository includes MZSelectableLabel that shows a simple use of the label in a storyboard with examples for implementing tappable links.

The demo also demonstrates how to use a gesture recognizer with the label to implement a long press on a link, which uses the rangeValueAtLocation method.

Requirements

MZSelectableLabel requires either iOS 7.x and above.

Contact

Michal Zaborowski

Twitter

Bitdeli Badge



MZSelectableLabel

一个简单的用于替代为iOS 7提供自动检测颜色的UILabel的下载。

data-canonical-src

如何使用

  1. Add the files MZSelectableLabel.m and MZSelectableLabel.h to your project.
  2. Design your user interface as you would normally. In Interface Builder set the custom class for any UILabel you want to replace to MZSelectableLabel. The label should honour all IB settings or create MZSelectableLabel objects in code.

附加:

您可以设置自动前景色检测:

@property (nonatomic, assign, getter = isAutomaticForegroundColorDetectionEnabled) IBInspectable BOOL automaticForegroundColorDetectionEnabled;
@property (nonatomic, strong) IBInspectable UIColor *skipColorForAutomaticDetection;

这意味着如果您已经归属字符串,MZSelectableLabel将检测您的所有范围。 即使您可以跳过默认颜色的检测,例如 [UIColor blackColor]

示例

[self.label setSelectableRange:[[self.label.attributedText string] rangeOfString:@"Tap me"] hightlightedBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.3]];

self.label.selectionHandler = ^(NSRange range, NSString *string) {

<span class="pl-c1">NSString</span> *message = [<span class="pl-c1">NSString</span> <span class="pl-c1">stringWithFormat:</span><span class="pl-s"><span class="pl-pds">@&#34;</span>You tapped <span class="pl-c1">%@</span><span class="pl-pds">&#34;</span></span>, string];
UIAlertView *alert = [[UIAlertView <span class="pl-c1">alloc</span>] <span class="pl-c1">initWithTitle:</span><span class="pl-s"><span class="pl-pds">@&#34;</span>Hello<span class="pl-pds">&#34;</span></span>
                                                <span class="pl-c1">message:</span>message
                                               <span class="pl-c1">delegate:</span><span class="pl-c1">nil</span>
                                      <span class="pl-c1">cancelButtonTitle:</span><span class="pl-s"><span class="pl-pds">@&#34;</span>Dismiss<span class="pl-pds">&#34;</span></span>
                                      <span class="pl-c1">otherButtonTitles:</span><span class="pl-c1">nil</span>];
[alert <span class="pl-c1">show</span>];

};

演示

Repository includes MZSelectableLabel that shows a simple use of the label in a storyboard with examples for implementing tappable links.

The demo also demonstrates how to use a gesture recognizer with the label to implement a long press on a link, which uses the rangeValueAtLocation method.

要求

MZSelectableLabel需要iOS 7.x及以上版本。

联系方式

Michal Zaborowski

Twitter

Bitdeli徽章




相关问题推荐