TTTAttributedLabel 使用方法
2017-06-01 11:24
1321 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel
#import "ViewController.h"
@interfaceViewController () <TTTAttributedLabelDelegate,UIActionSheetDelegate>
@end
@implementation ViewController
static
inline NSRegularExpression * NameRegularExpression() {
static
NSRegularExpression *_nameRegularExpression = nil;
static
dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpressionalloc]
initWithPattern:@"^\\w+"options:NSRegularExpressionCaseInsensitiveerror:nil];
});
return _nameRegularExpression;
}
- (void)viewDidLoad {
[superviewDidLoad];
TTTAttributedLabel *label = [[TTTAttributedLabelalloc]
initWithFrame:CGRectMake(100,120,
120,60)];
label.font = [UIFontsystemFontOfSize:14];
label.textColor = [UIColorblackColor];
label.lineBreakMode =NSLineBreakByCharWrapping;
label.numberOfLines =0;
//设置高亮颜色
label.highlightedTextColor = [UIColorgreenColor];
//检测url
label.enabledTextCheckingTypes =NSTextCheckingTypeLink;
//对齐方式
label.verticalAlignment =TTTAttributedLabelVerticalAlignmentCenter;
//行间距
label.lineSpacing =8;
//设置阴影
label.shadowColor = [UIColorgrayColor];
label.delegate =self;
// Delegate
//NO
不显示下划线
label.linkAttributes = [NSDictionarydictionaryWithObject:[NSNumber
numberWithBool:NO]forKey:(NSString *)kCTUnderlineStyleAttributeName];
NSString *text =@"冷清风
赞了 王战 的说说";
[label setText:textafterInheritingLabelAttributesAndConfiguringWithBlock:^
NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
{
//设置可点击文字的范围
NSRange boldRange = [[mutableAttributedStringstring]
rangeOfString:@"冷清风"options:NSCaseInsensitiveSearch];
//设定可点击文字的的大小
UIFont *boldSystemFont = [UIFontboldSystemFontOfSize:16];
CTFontRef font =CTFontCreateWithName((__bridgeCFStringRef)boldSystemFont.fontName,
boldSystemFont.pointSize,NULL);
if (font) {
//设置可点击文本的大小
[mutableAttributedString
addAttribute:(NSString *)kCTFontAttributeNamevalue:(__bridge
id)font range:boldRange];
//设置可点击文本的颜色
[mutableAttributedStringaddAttribute:(NSString*)kCTForegroundColorAttributeNamevalue:(id)[[UIColorblueColor]
CGColor] range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
//正则
NSRegularExpression *regexp =NameRegularExpression();
NSRange linkRange = [regexprangeOfFirstMatchInString:text
options:0range:NSMakeRange(0,3)];
NSURL *url = [NSURLURLWithString:[NSString
stringWithFormat:@"http://www.exiucai.com/"]];
//设置链接的url
[label addLinkToURL:urlwithRange:linkRange];
[self.viewaddSubview:label];
}
- (void)attributedLabel:(__unusedTTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[[[UIActionSheetalloc]
initWithTitle:[url absoluteString]
delegate:selfcancelButtonTitle:NSLocalizedString(@"Cancel",nil)
destructiveButtonTitle:nilotherButtonTitles:NSLocalizedString(@"Open Link in Safari",nil),
nil]showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
[[UIApplicationsharedApplication]
openURL:[NSURLURLWithString:actionSheet.title]];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel
#import "ViewController.h"
@interfaceViewController () <TTTAttributedLabelDelegate,UIActionSheetDelegate>
@end
@implementation ViewController
static
inline NSRegularExpression * NameRegularExpression() {
static
NSRegularExpression *_nameRegularExpression = nil;
static
dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpressionalloc]
initWithPattern:@"^\\w+"options:NSRegularExpressionCaseInsensitiveerror:nil];
});
return _nameRegularExpression;
}
- (void)viewDidLoad {
[superviewDidLoad];
TTTAttributedLabel *label = [[TTTAttributedLabelalloc]
initWithFrame:CGRectMake(100,120,
120,60)];
label.font = [UIFontsystemFontOfSize:14];
label.textColor = [UIColorblackColor];
label.lineBreakMode =NSLineBreakByCharWrapping;
label.numberOfLines =0;
//设置高亮颜色
label.highlightedTextColor = [UIColorgreenColor];
//检测url
label.enabledTextCheckingTypes =NSTextCheckingTypeLink;
//对齐方式
label.verticalAlignment =TTTAttributedLabelVerticalAlignmentCenter;
//行间距
label.lineSpacing =8;
//设置阴影
label.shadowColor = [UIColorgrayColor];
label.delegate =self;
// Delegate
//NO
不显示下划线
label.linkAttributes = [NSDictionarydictionaryWithObject:[NSNumber
numberWithBool:NO]forKey:(NSString *)kCTUnderlineStyleAttributeName];
NSString *text =@"冷清风
赞了 王战 的说说";
[label setText:textafterInheritingLabelAttributesAndConfiguringWithBlock:^
NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
{
//设置可点击文字的范围
NSRange boldRange = [[mutableAttributedStringstring]
rangeOfString:@"冷清风"options:NSCaseInsensitiveSearch];
//设定可点击文字的的大小
UIFont *boldSystemFont = [UIFontboldSystemFontOfSize:16];
CTFontRef font =CTFontCreateWithName((__bridgeCFStringRef)boldSystemFont.fontName,
boldSystemFont.pointSize,NULL);
if (font) {
//设置可点击文本的大小
[mutableAttributedString
addAttribute:(NSString *)kCTFontAttributeNamevalue:(__bridge
id)font range:boldRange];
//设置可点击文本的颜色
[mutableAttributedStringaddAttribute:(NSString*)kCTForegroundColorAttributeNamevalue:(id)[[UIColorblueColor]
CGColor] range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
//正则
NSRegularExpression *regexp =NameRegularExpression();
NSRange linkRange = [regexprangeOfFirstMatchInString:text
options:0range:NSMakeRange(0,3)];
NSURL *url = [NSURLURLWithString:[NSString
stringWithFormat:@"http://www.exiucai.com/"]];
//设置链接的url
[label addLinkToURL:urlwithRange:linkRange];
[self.viewaddSubview:label];
}
- (void)attributedLabel:(__unusedTTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[[[UIActionSheetalloc]
initWithTitle:[url absoluteString]
delegate:selfcancelButtonTitle:NSLocalizedString(@"Cancel",nil)
destructiveButtonTitle:nilotherButtonTitles:NSLocalizedString(@"Open Link in Safari",nil),
nil]showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
[[UIApplicationsharedApplication]
openURL:[NSURLURLWithString:actionSheet.title]];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
相关文章推荐
- TTTAttributedLabel 使用方法 (可点击文字文本)
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- iOS学笔记本45- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- 【Swift】TTTAttributedLabel使用小记
- iOS TTTAttributedLabel 使用
- TTTAttributedLabel 使用
- TTTAttributedLabel的使用
- DTAttributedLabel 使用方法
- TTTAttributedLabel使用介绍(转)
- TTTAttributedLabel 使用小记
- TTTAttributedLabel第三方使用(一段连续的字符串不同范围用不同字体和颜色)
- TTTAttributedLabel 的点击方法
- Cocos2d-x游戏开发之CCLabelTTF标签详解之Create方法(设置自动换行的标签)和换行符的巧妙使用(十六)(高级篇) (转)
- google map api v3 的marker使用label的方法(markerwithlabel的使用)
- NSMutableAttributedString 的使用方法,设置格式
- TTTAttributedLabel