一段文本内容中展示网址链接---TTTAttributedLabel链接点击和textView两种方式实现
来源:互联网 发布:windows vim编辑器 编辑:程序博客网 时间:2024/06/05 10:57
上一篇关联文章:富文本上 http://blog.csdn.net/liyunxiangrxm/article/details/53410919
// Copyright © 2016年 liyunxiang. All rights reserved.//#import "ViewController.h"#import "TTTAttributedLabel.h"#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]@interface ViewController ()<TTTAttributedLabelDelegate>@end@implementation ViewController- (void)viewDidLoad{ [self test2];}-(void)test2{ [super viewDidLoad]; UITextView *ContentTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)]; ContentTextView.editable = NO; ContentTextView.dataDetectorTypes = UIDataDetectorTypeAll; ContentTextView.scrollEnabled = NO; ContentTextView.textColor = UIColorFromRGB(0x333333); NSString *text = [NSString stringWithFormat:@"https://github.com/TTTAttributedLabel/TTTAttributedLabel asifuhaskjf;lks http://www.baidu.com 15754713678 ��"];// ContentTextView.text = text; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineHeightMultiple = 10.f; paragraphStyle.maximumLineHeight = 16.0f; paragraphStyle.minimumLineHeight = 14.0f; paragraphStyle.firstLineHeadIndent = 0.0f; paragraphStyle.lineSpacing = 1.0f; paragraphStyle.alignment = NSTextAlignmentJustified; UIFont *font = [UIFont fontWithName:@"Arial" size:15]; NSDictionary *attributes = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle}; ContentTextView.attributedText = [[NSAttributedString alloc]initWithString:text attributes:attributes]; [self.view addSubview:ContentTextView];}-(void)test3{ [super viewDidLoad];// self.view.backgroundColor = [UIColor yellowColor]; // modify by huangyibiao //添加文本信息 TTTAttributedLabel *bubbleText = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0,100,300,300)]; bubbleText.delegate = self;// bubbleText.tag=indexPath.row; UIFont *font = [UIFont systemFontOfSize:15]; bubbleText.font = font; bubbleText.numberOfLines = 0; bubbleText.lineBreakMode = NSLineBreakByWordWrapping; NSString *text = [NSString stringWithFormat:@"东方会计师;老地方就卡死;来的房间爱死了;地方水立方的看见爱上对方http://www.baidu.com了;贾师傅;拉士大夫了;按时交电费https://github.com/TTTAttributedLabel/TTTAttributedLabel sfasdfasdfasfdas爱就是看了对方那算了的疯狂"]; bubbleText.text = text; bubbleText.linkAttributes = @{(NSString *)kCTUnderlineStyleAttributeName : [NSNumber numberWithBool:YES], (NSString*)kCTForegroundColorAttributeName : (id)[[UIColor blueColor] CGColor]}; bubbleText.highlightedTextColor = [UIColor whiteColor]; bubbleText.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop; // end modify by huangyibiao // reasion: handle links in chat content, ananylizing each link // 提取出文本中的超链接 NSError *error; NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:text options:0 range:NSMakeRange(0, [text length])]; NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text]; for (NSTextCheckingResult *match in arrayOfAllMatches) { NSString *substringForMatch = [text substringWithRange:match.range]; [attribute addAttribute:(NSString *)kCTFontAttributeName value:(id)bubbleText.font range:match.range]; [attribute addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:match.range]; [bubbleText addLinkToURL:[NSURL URLWithString:substringForMatch] withRange:match.range]; } [self.view addSubview:bubbleText]; bubbleText.userInteractionEnabled = YES; UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressText:)]; [bubbleText addGestureRecognizer:longTap]; // end add by huangyibiao}- (void)longPressText:(UILongPressGestureRecognizer *)sender{ if (sender.state == UIGestureRecognizerStateBegan){ TTTAttributedLabel *label = (TTTAttributedLabel *)sender.view; NSLog(@"%@",label.text);}}#pragma mark - TTTAttributedLabelDelegate 点击聊天内容中的超链接- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { NSLog(@"asdfasd");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
4 0
- 一段文本内容中展示网址链接---TTTAttributedLabel链接点击和textView两种方式实现
- TTTAttributedLabel链接点击和textView--前传
- 【iOS】TTTAttributedLabel链接点击
- TextView中的文本链接之--链接的几种方式
- TextView添加链接实现富文本label带点击功能
- 监听TextView中链接文本的点击事件
- Android中点击TextView文本链接跳转到指定页面
- (转)监听TextView中链接文本的点击事件
- 给TextView中的部分文本添加链接的两种方式
- 在Textview中获取指定文字位置(兼顾网址链接和emoji表情),并在其附近展示图片
- android---TextView中电话号码、网址自动链接的实现方法
- Android---10---TextView添加链接的两种方式
- jquery两种方式实现链接失效
- jquery两种方式实现链接失效
- android textview 自动链接网址 修改默认点击事件
- android textview 自动链接网址 修改默认点击事件
- Android 实现TextView中 文字链接的方式
- android:TextView中的文本链接之--链接的几种方式
- Websocket协议的学习、调研和实现
- 正则表达式抓取网页资源
- 阿里云Ubuntu中的Python版本管理
- POJ - 2002(静态哈希链表找正方形)
- Office 2013 Powerpoint 打开时出现 VBE6EXT.OLB不能被加载
- 一段文本内容中展示网址链接---TTTAttributedLabel链接点击和textView两种方式实现
- 调度器(scheduler(摘自官方文档)
- 二十种特征变换方法及Spark MLlib调用实例(Scala/Java/python)(二)
- 面试推理题,发挥脑洞,求解
- eclipse或者myeclipse中debug模式下启动慢解决方案
- 利用redis缓存对 list集合中的数据 进行分页操作
- 一个故事讲清楚NIO
- UVA 11082 Matrix Decompressing
- 接口是否可以继承接口;抽象类是否可以实现接口;抽象类是否可以继承实体类?