上一篇关联文章:富文本上 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