TTTAttributedLabel
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:nil otherButtonTitles: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.
}
TTTAttributedLabel的更多相关文章
- TTTAttributedLabel 富文本小记
- (void)setupTipsLabel:(TTTAttributedLabel *)label { UIColor *red = [UIColor mainColor]; UIColor *gr ...
- TTTAttributedLabel xib sb lineSpacing not working
https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/733 set the same text in storyboard ...
- 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题
前言 还以为是自己代码写的有问题,用法和别的地方都一样,但是这个是在 UITableViewCell 中使用,另外在 tableHeaderView 中使用也没用这个问题 —— 使用 TTTAttri ...
- 【Swift】TTTAttributedLabel使用小记
前言 TTTAttributedLabel继承自UILabel,很方便基于现有代码进行修改,Star超过4K+,今天用了一下作点笔记. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http: ...
- TTTAttributedLabel 如何将多个字符串高亮显示
TTTAttributedLabel进行多个字符串的高亮显示. 需要对每个字符串进行匹配,从而得到所有需要高亮的NSRange,然后利用NSMutableAttributedString对每个NSRa ...
- TTTAttributedLabel使用介绍(转)
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 可以实现电话 地址 链接自动查找显示 ...
- 用TTTAttributedLabel创建变化丰富的UILabel
转自:http://blog.csdn.net/prevention/article/details/9998575 1. 不同颜色的字段混合在一个Label里怎么实现? 看TTTAttributed ...
- TTTAttributedLabel颜色设置
NSString *text = @"Lorem ipsum dolor sit amet"; [self.testAttributedLabel setText:text aft ...
- iOS 开发富文本之TTTAttributedLabel 在某个特定位置的文字添加跳转,下划线,修改字体大小,颜色
@property(nonatomic , strong) TTTAttributedLabel * ttLabel; @property(nonatomic , strong) NSRange li ...
随机推荐
- 网页DIV+CSS布局和动画美化全程实例 (陈益材) 随书光盘
网站的建站技术近几年得到迅速的发展,网页的布局与特效动画技术层出不穷,网站建设已经从简单的技术支持时代衍变到现在的视觉美化时代.但如何使设计的网页高人一筹,达到让人过目不忘的境界,如何追求以最简单的特 ...
- streaming kafka direct 详解
http://blog.cloudera.com/blog/2015/03/exactly-once-spark-streaming-from-apache-kafka/ http://www.jia ...
- JDK1.8源码(十一)——java.util.TreeMap类
在前面几篇博客分别介绍了这样几种集合,基于数组实现的ArrayList 类,基于链表实现的LinkedList 类,基于散列表实现的HashMap 类,本篇博客我们来介绍另一种数据类型,基于树实现的T ...
- PHP中的继承
<?php class Bar { private $salary = 3000; public $lunch = 1000; // php中关于“可见性”的概念 public function ...
- ejs使用文档
EJS是一个javascript模板库,用来从json数据中生成HTML字符串. 功能:缓存功能,能够缓存好的HTML模板: <% code %>用来执行javascript代码 ejs模 ...
- 从零开始安装 Ambari (2) -- 准备本地 repository
安装 Ambari,最后是为了用它部署 hadoop 集群.安装时默认使用的是 hortonworks 远程的资源,用它部署集群时,需要下载 Hadoop.Hive.HBase 的安装包,速度很慢.我 ...
- 5.Python初窥门径(字典)
Python字典学习 1.字典初识 1.字典的简单介绍 字典(dict),是python中唯一的映射类型.他是以{ }括起来的键值对组成.在dict中key是唯一的.在保存的时候,根据key来 ...
- linux环境下jdk部署配置
1.java官网下载相关的jdk包 2.配置系统环境变量,编辑/etc/profile文件,在文件的末尾添加一下信息: export JAVA_HOME=/usr/jdk1.8.0_101export ...
- State模式(状态设计模式)
State??? State模式中,我们用类来表示状态.以类来表示状态后,我们就能通过切换类来方便地改变对象的状态.当需要增加新的状态时,如何修改代码这个问题也会很明确. 直接用状态代替硬编码 依赖于 ...
- ContOS7分区并挂载硬盘(gpt)
parted fdisk [只支持MSDOS分区布局] parted [支持MSDOS.GPT分区布局] 分区有三个步骤: 第一个步骤就是用分区工具进行分区 第二个步骤就是创建文件系统(也就是格式化) ...