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. }

最新文章

  1. linux日常易忘指令
  2. Javascript小笔记
  3. 总结html5-canvas学习笔记
  4. CentOS下Redis 2.2.14安装配置详解(转载)
  5. Getting Started with the NDK
  6. 记一次SQL联合查询注入工具的编写
  7. Linux 文件系统(二)---运行过程及结构间的关系
  8. bootstrap 获得轮播中的索引或当前活动的焦点对象
  9. POJ 2359 Questions(约瑟夫环——数学解法)
  10. 《JavaScript高级程序设计》笔记:DOM(十)
  11. 长图的展开与收起(Android)
  12. BBS论坛(二十一)
  13. Building QGIS from source - step by step(随笔3)
  14. [dart学习]第一篇:windows下安装配置dart编译环境,写出helloworld
  15. EasyUI中使用textbox赋值,setValue和setText顺序问题
  16. huffman(greedy)
  17. Java用户名登录学生信息管理系统并对其进行增删改查操作
  18. dedecms前端无法调用自定义变量怎么解决
  19. Haskell语言学习笔记(47)Arrow(2)
  20. windows多线程(七) 事件event

热门文章

  1. 浅谈.net的后台校验
  2. Spark BlockManager 概述
  3. 51nod1489(dfs)
  4. 清北刷题冲刺 10-31 p.m
  5. 数组API(2)
  6. SpringBoot整合MyBatis之xml配置
  7. AT2166 Rotate 3x3
  8. thinkphp5文件上传问题
  9. mysql update select 从查询结果中更新数据
  10. HDU6438:Buy and Resell(贪心+数据结构)