iOS学笔记本45- TTTAttributedLabel 使用方法
2015-11-18 14:57
721 查看
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel
#import "ViewController.h"
@interface ViewController () <TTTAttributedLabelDelegate, UIActionSheetDelegate>
@end
@implementation ViewController
static inline NSRegularExpression * NameRegularExpression() {
static NSRegularExpression *_nameRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _nameRegularExpression;
}
- (void)viewDidLoad {
[super viewDidLoad];
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(100, 120, 120, 60)];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor blackColor];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
//设置高亮颜色
label.highlightedTextColor = [UIColor greenColor];
//检测url
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//对齐方式
label.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
//行间距
label.lineSpacing = 8;
//设置阴影
label.shadowColor = [UIColor grayColor];
label.delegate = self; // Delegate
//NO 不显示下划线
label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
NSString *text = @"冷清风 赞了 王战 的说说";
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString*mutableAttributedString)
{
//设置可点击文字的范围
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"冷清风" options:NSCaseInsensitiveSearch];
//设定可点击文字的的大小
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
//设置可点击文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
//设置可点击文本的颜色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
//正则
NSRegularExpression *regexp = NameRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(0, 3)];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.exiucai.com/"]];
//设置链接的url
[label addLinkToURL:url withRange:linkRange];
[self.view addSubview:label];
}
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[[[UIActionSheet alloc] initWithTitle:[url absoluteString] delegate:self cancelButtonTitle: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;
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionSheet.title]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "ViewController.h"
@interface ViewController () <TTTAttributedLabelDelegate, UIActionSheetDelegate>
@end
@implementation ViewController
static inline NSRegularExpression * NameRegularExpression() {
static NSRegularExpression *_nameRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _nameRegularExpression;
}
- (void)viewDidLoad {
[super viewDidLoad];
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(100, 120, 120, 60)];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor blackColor];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
//设置高亮颜色
label.highlightedTextColor = [UIColor greenColor];
//检测url
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//对齐方式
label.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
//行间距
label.lineSpacing = 8;
//设置阴影
label.shadowColor = [UIColor grayColor];
label.delegate = self; // Delegate
//NO 不显示下划线
label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
NSString *text = @"冷清风 赞了 王战 的说说";
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString*mutableAttributedString)
{
//设置可点击文字的范围
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"冷清风" options:NSCaseInsensitiveSearch];
//设定可点击文字的的大小
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
//设置可点击文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
//设置可点击文本的颜色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
//正则
NSRegularExpression *regexp = NameRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(0, 3)];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.exiucai.com/"]];
//设置链接的url
[label addLinkToURL:url withRange:linkRange];
[self.view addSubview:label];
}
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[[[UIActionSheet alloc] initWithTitle:[url absoluteString] delegate:self cancelButtonTitle: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;
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionSheet.title]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
相关文章推荐
- iOS开发库的列表
- 实现iOS7以前的TableViewGroupStyle的方法。
- iOS viewDidLayoutSubviews,viewdidload
- 苹果开发者账号申请流程--企业
- iOS审核秘籍:应用内容检查大法
- IOS6和IOS7的适配问题
- iOS应用发布中的一些细节
- iOS开发代码自动布局NSLayoutConstraint
- iOS GCD创建标准定时器,进入后台后任可继续定时,无需开启后台任务
- 利用NSCache提升效率
- iOS ---对json数组的解析
- nagios_client_V2_sh
- iOS: Crash文件解析
- IOS-使用系统默认声音、震动
- iOS开发 -- 添加自定义ttf字体的方法
- iOS菜鸟之苹果开发者账号的注册
- 自定义iOS下拉框
- iOS开发:为xcode项目添加git仓储
- IOS学习之__FUNCTION__的作用
- 好文推荐:iOS开发中的Git流程