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的更多相关文章

  1. TTTAttributedLabel 富文本小记

    - (void)setupTipsLabel:(TTTAttributedLabel *)label { UIColor *red = [UIColor mainColor]; UIColor *gr ...

  2. TTTAttributedLabel xib sb lineSpacing not working

    https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/733 set the same text in storyboard ...

  3. 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题

    前言 还以为是自己代码写的有问题,用法和别的地方都一样,但是这个是在 UITableViewCell 中使用,另外在 tableHeaderView 中使用也没用这个问题 —— 使用 TTTAttri ...

  4. 【Swift】TTTAttributedLabel使用小记

    前言 TTTAttributedLabel继承自UILabel,很方便基于现有代码进行修改,Star超过4K+,今天用了一下作点笔记. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http: ...

  5. TTTAttributedLabel 如何将多个字符串高亮显示

    TTTAttributedLabel进行多个字符串的高亮显示. 需要对每个字符串进行匹配,从而得到所有需要高亮的NSRange,然后利用NSMutableAttributedString对每个NSRa ...

  6. TTTAttributedLabel使用介绍(转)

    TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 可以实现电话  地址  链接自动查找显示 ...

  7. 用TTTAttributedLabel创建变化丰富的UILabel

    转自:http://blog.csdn.net/prevention/article/details/9998575 1. 不同颜色的字段混合在一个Label里怎么实现? 看TTTAttributed ...

  8. TTTAttributedLabel颜色设置

    NSString *text = @"Lorem ipsum dolor sit amet"; [self.testAttributedLabel setText:text aft ...

  9. iOS 开发富文本之TTTAttributedLabel 在某个特定位置的文字添加跳转,下划线,修改字体大小,颜色

    @property(nonatomic , strong) TTTAttributedLabel * ttLabel; @property(nonatomic , strong) NSRange li ...

随机推荐

  1. Fiddler开启Https的时候出现unable to configure windows to trust Fiddler Root certificate问题

    前言 通过log页面看到错误为:访问控制列表(ACL)结构无效. 网上(baidu,bing,google)各种方式都试过了: 如重置证书(Reset all certificates) 导出证书到本 ...

  2. Sharding-JDBC 使用入门和基本配置

    一.什么是Sharding-JDBC Sharding-JDBC定位为轻量级Java框架,在Java的JDBC层提供的额外服务.它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理 ...

  3. [Windows]获取系统版本号

    1 string GetMainProgInfo() 2 { 3 string strRet; 4 TCHAR szPath[MAX_PATH]; 5 GetModuleFileName(NULL,s ...

  4. ZOJ 2849【瞎暴力的搜索】

    思路: 靠评测机抖一抖的思路: 拿个队列维护一下符合类型的可以搜索(指四周还存在可以遍历的点)的点.然后暴力搜索,所以问题来了,这个暴力搜索会大大地重复遍历次数. DFS遍历图以前一直忽略重复,以为搜 ...

  5. Claris’ Contest # 2 Day 2 Problem C. Dash Speed(分治+可持久化并查集+树剖)

    题面 题解 \(std\)爆栈了→_→ 我们先考虑一个简化的问题,如果只有加边的情况下如何动态维护直径 合并两棵树时,设\(a,b\)为\(A\)的直径的两个端点,\(c,d\)为\(B\)的直径的两 ...

  6. plpython 中文分词Windows下 PG数据库jieba分词

    windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...

  7. 一切从这里起始(左耳听风 ARTS 6号小组 week 1)

    ARTS 具体要求: 1.每周至少做一个 leetcode 的算法题2.阅读并点评至少一篇英文技术文章3.学习至少一个技术技巧4.分享一篇有观点和思考的技术文章 1.Algorithm Two Sum ...

  8. chromedriver对应chrom版本

    chromedriver版本 支持的Chrome版本 v2.37 v64-66 v2.36 v63-65 v2.35 v62-64 v2.34 v61-63 v2.33 v60-62 v2.32 v5 ...

  9. PAT甲级——1094 The Largest Generation (树的遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...

  10. 使用jmeter往指定文件中插入一定数量的数据(转)

    有一个需求,新建一批账号,把获取的账号相关信息存入文本文件,当文本文件保存的数据达到一定的数量,就自动停止新建账号. 分析下需求: 1.把账号信息保存到文件,需要使用bean shell脚本(bean ...