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 ...
随机推荐
- Jquery常用标签
$(this).hide(1000)//隐藏该元素 $(this).show(1000)//显示该元素 $(this).fadeIn(1000)//淡入已隐藏的元素 $(this).fadeOut(1 ...
- 【Strtus2】
基于mvc设计模式的web应用框架!strtus2作为控制器来建立模型与视图数据的交互.
- EIP权限工作流平台总结-3后端框架
1.预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...
- 数据库路由中间件MyCat - 源代码篇(16)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 5. 路由模块 真正取得RouteResultset的步骤:AbstractRouteStrategy的ro ...
- WebGL three.js学习笔记 6种类型的纹理介绍及应用
WebGL three.js学习笔记 6种类型的纹理介绍及应用 本文所使用到的demo演示: 高光贴图Demo演示 反光效果Demo演示(因为是加载的模型,所以速度会慢) (一)普通纹理 计算机图形学 ...
- centos lamp 配置
# 1. 查看是否有httpd进程正在运行(下图是有的情况) ps -ef|grep httpd
- oracle批量插入带主键自增
https://blog.csdn.net/qq_37630354/article/details/82792288
- 《权限系列shiro+cas》---封装公共验证模块
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 我们现在可以将任意属性传递给每个顶点的顶点着色器使用.但是 ...
- javascript的模块发展
谨以此文记录了解js模块的过程 随着ES6的出现,js模块已经成为正式的标准了.曾经为了解决js模块问题而发展起来的民间秘籍,requireJs(AMD).SeaJs(CMD).Node(Common ...
- POJ1032 Parliament
题目来源:http://poj.org/problem?id=1032 题目大意:给定一个正整数N(5<=N<=1000),将N拆为若干个不同的数使得它们的乘积最大(找到一组互不相等,和为 ...