响应式标签 1.0.11

响应式标签 1.0.11

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布上一个版本2016年4月

hsusmita维护。



  • 作者:
  • Susmita Horrow

一个响应触摸指定模式的UILabel子类。它有以下特点:

  1. 它可以检测正则表达式指定的模式并应用样式,如字体、颜色等。
  2. 允许用可点击的.attributed string替换默认的省略号以标记截断
  3. 提供了方便的方法来检测哈希标签、用户名处理器和URL

安装

在您的pod文件中添加以下行:
pod 'ResponsiveLabel', '~> 1.0.10'

用法

以下片段解释了公共方法的用法。这些片段假定有一个名为"customLabel"的ResponsiveLabel实例。

#import <ResponsiveLabel.h>

在Interface Builder中,将您的UILabel的定制类设置为ResponsiveLabel。您可能会收到一条错误消息,说"错误:IB Designables:无法更新自动布局状态:无法从路径(null)加载设计ables"。这似乎是Xcode和CocoaPods的一个问题,并且似乎不会引起任何问题,但有些人已经设法修复了它,有关更多详细信息,请参阅此Stackoverflow问题

模式检测

//Detects email in text
NSString *emailRegexString = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:emailRegexString
options:0
error:&error];
PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex withSearchType:PatternSearchTypeAll 
withPatternAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
[self.customLabel enablePatternDetection:descriptor];

字符串检测

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder tapResponder = ^(NSString *string) {
    NSLog(@"tapped = %@",string);
};
[self.customLabel enableStringDetection:@"text" withAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
                                                                 RLTapResponderAttributeName: tapResponder}];

字符串数组检测

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder stringTapAction = ^(NSString *tappedString) {
    NSLog(@"tapped string = %@",tappedString);
  };
[self.customLabel enableDetectionForStrings:@[@"text",@"long"] withAttributes:@{NSForegroundColorAttributeName:[UIColor brownColor],
                                                                                  RLTapResponderAttributeName:stringTapAction}];

哈希标签检测

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder hashTagTapAction = ^(NSString *tappedString) {
NSLog(@"HashTag Tapped = %@",tappedString);
};
[self.customLabel enableHashTagDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor redColor], RLTapResponderAttributeName:hashTagTapAction}];

用户名处理器检测

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder userHandleTapAction = ^(NSString *tappedString){
NSLog(@"Username Handler Tapped = %@",tappedString);
};
[self.customLabel enableUserHandleDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor grayColor],RLTapResponderAttributeName:userHandleTapAction}];

URL检测

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder urlTapAction = ^(NSString *tappedString) {
NSLog(@"URL Tapped = %@",tappedString);
};
[self.customLabel enableURLDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor cyanColor],NSUnderlineStyleAttributeName:[NSNumber
numberWithInt:1],RLTapResponderAttributeName:urlTapAction}];

触摸时突出显示模式

要突出显示模式,可以设置属性:

  • RLHighlightedForegroundColorAttributeName
  • RLHighlightedBackgroundColorAttributeName
self.customLabel.userInteractionEnabled = YES;
PatternTapResponder userHandleTapAction = ^(NSString *tappedString){
NSLog(@"Username Handler Tapped = %@",tappedString);
};
[self.customLabel enableUserHandleDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor grayColor],RLHighlightedForegroundColorAttributeName:[UIColor greenColor],RLHighlightedBackgroundColorAttributeName:[UIColor blackColor],RLTapResponderAttributeName:userHandleTapAction}];

自定义截断令牌

将.attributed string设置为截断令牌
在1.0.10中弃用
NSString *expansionToken = @"Read More ...";
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}];
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) {
NSLog(@"Tap on truncation text");
}];
[self.customLabel setText:str withTruncation:YES];
最新引入于1.0.10
NSString *expansionToken = @"Read More ...";
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
PatternTapResponder tap = ^(NSString *string) {
   NSLog(@"Tap on truncation text");
  }
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font,RLTapResponderAttributeName:tap}];
[self.customLabel setAttributedTruncationToken:attribString];
[self.customLabel setText:str withTruncation:YES];
将图片设置为截断标记

图片大小的高度应当大致等于或小于字体高度。否则图片将无法正确显示。

[self.customLabel setTruncationIndicatorImage:[UIImage imageNamed:@"more_image"] withSize:CGSizeMake(25, 5) andAction:^(NSString *tappedString) {
    NSLog(@"tapped on image");
 }];
从界面构建器设置

截图

参考

ResponsiveLabel的底层实现基于KILabel(《https://github.com/Krelborn/KILabel》),ResponsiveLabel经过灵活设计,以支持检测由正则表达式指定的任何模式。

以下文章有助于增强功能: