TTTAttributedLabel 使用方法 (可点击文字文本)
来源:互联网 发布:淘宝怎么运营 编辑:程序博客网 时间:2024/06/05 00:13
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel#import "ViewController.h"@interface ViewController () <TTTAttributedLabelDelegate, UIActionSheetDelegate>@end@implementation ViewControllerstatic 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:nil otherButtonTitles:NSLocalizedString(@"Open Link in Safari", nil), nil] showInView:self.view];}#pragma mark - UIActionSheetDelegate- (void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(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.}
转载自:http://blog.csdn.net/lengshengren/article/details/43971441
0 0
- TTTAttributedLabel 使用方法 (可点击文字文本)
- 点击文字变成可编辑的文本
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- TTTAttributedLabel 使用方法
- 【iOS】TTTAttributedLabel链接点击
- UILabel文字可点击
- 实现文本可点击
- 一段文本内容中展示网址链接---TTTAttributedLabel链接点击和textView两种方式实现
- TextView部分文字可点击
- LinkTextView-可点击的文本
- iOS学笔记本45- TTTAttributedLabel 使用方法
- TTTAttributedLabel链接点击和textView--前传
- TextView部分文字变色、可点击。(微博话题 变色、点击)
- 使用SpannableString,ClickableSpan,文字部分可点击
- java设计模式—工厂模式三种
- 时间戳
- QDialog accept()函数 和 QCloseEvent的accept()
- MAC 设置环境变量path的几种方法
- Android Studio跟Eclipse中编译完后产生apk的位置
- TTTAttributedLabel 使用方法 (可点击文字文本)
- 启用多处理器编译--加快VS2013编译
- angularjs之手机端input图片上传
- iOS利用正则表达式验证邮箱格式是否正确
- 把数据库中的表中信息复制到另一个表中
- 单例模式和简单工厂模式
- java 访问http
- 桃花庵记
- Chisel 专题(一) 安装