Instantly share code, notes, and snippets.

Embed
What would you like to do?
shanshuo
#import "TestViewController.h"
@interface TestViewController ()<TYLabelDelegate>
@property (nonatomic,copy) NSString *text;
@property (nonatomic,copy) NSString *text2;
@property (nonatomic,strong) TYLabel *contentAttributedLabel;
@property (nonatomic,strong) TYTextRender *textRender;
@property (nonatomic,strong) TYTextRender *textRender2;
@property (nonatomic,assign) CGFloat contentHeight;
@property (nonatomic,assign) CGFloat contentHeight2;
@property (strong,nonatomic)NSArray *emotionArray;//表情数组
@property (strong,nonatomic)NSArray *atPersonArray;//at
@property (strong,nonatomic)NSArray *urlArray;//网址数组
@property (strong,nonatomic)NSArray *topicArray;//话题
@property (nonatomic,assign) BOOL isRender2;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.text = @"拍照怎么搜题(上) http://t.cn/RqUt0Ap by 简单的老王 分享自 @Morgan 创建的《iMorgan》 http://t.cn/RU6oSpV\
( 想看更多?下载 @开发者头条 app:http://t.cn/RA0H2i6 )";
self.text2 = @"@kimsungwhee:#KSHObjcUML# 2.0发布,添加支持Swift项目类引用关系图,Swift项目请使用Swift-UML菜单 http://t.cn/RwJjlo2 已安装的朋友请从新安装更新到最新版本 #Xcode插件# @开发者头条 @唐巧_boy @SwiftGG翻译组 @CocoaChina @叶孤城___ @github精选 @SwiftLanguage @好东西传送门 @onevcat ";
[self parseAllKeywords];
_contentAttributedLabel=[[TYLabel alloc]init];
//_contentAttributedLabel.displaysAsynchronously = NO;
_contentAttributedLabel.lineSpacing = 20;
_contentAttributedLabel.delegate=self;
[self.view addSubview:_contentAttributedLabel];
[self calculateHegihtAndAttributedString:self.text isText2:NO];
[self calculateHegihtAndAttributedString:self.text2 isText2:YES];
self.contentAttributedLabel.frame = CGRectMake(CELL_SIDEMARGIN, 80, CGRectGetWidth(self.view.frame) - CELL_SIDEMARGIN * 2, self.contentHeight);
self.contentAttributedLabel.textRender = self.textRender;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, UIScreen.mainScreen.bounds.size.height - 60, 100, 44);
[button addTarget:self action:@selector(changeTextRender:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
}
- (void)changeTextRender:(id)sender {
self.isRender2 = !self.isRender2;
if (self.isRender2) {
self.contentAttributedLabel.textRender = self.textRender2;
self.contentAttributedLabel.frame = CGRectMake(CELL_SIDEMARGIN, 80, CGRectGetWidth(self.view.frame) - CELL_SIDEMARGIN * 2, self.contentHeight2);
} else {
self.contentAttributedLabel.textRender = self.textRender;
self.contentAttributedLabel.frame = CGRectMake(CELL_SIDEMARGIN, 80, CGRectGetWidth(self.view.frame) - CELL_SIDEMARGIN * 2, self.contentHeight);
}
}
-(void)calculateHegihtAndAttributedString:(NSString *)srctext isText2:(BOOL)flag
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:srctext];
text.ty_color = RGBCOLOR(93, 93, 93) ;
// : RGBCOLOR(51, 51, 51);
//at
for (NSInteger i=0;i<self.atPersonArray.count;++i)
{
WBKeywordModel *keywordModel=[self.atPersonArray objectAtIndex:i];
TYTextAttribute *textAttribute = [[TYTextAttribute alloc]init];
textAttribute.color = RGBCOLOR(82, 126, 173);;
TYTextHighlight *linkTextStorage=[[TYTextHighlight alloc]init];
linkTextStorage.backgroundColor = RGBCOLOR(191, 223, 254);
linkTextStorage.userInfo=@{@"url":keywordModel.url};
linkTextStorage.backgroudInset = UIEdgeInsetsMake(1, 0, 1, 1);
[text addTextAttribute:textAttribute range:keywordModel.range];
[text addTextHighlightAttribute:linkTextStorage range:keywordModel.range];
}
//话题
for (NSInteger i=0; i<self.topicArray.count;++i)
{
WBKeywordModel *keywordModel=[self.topicArray objectAtIndex:i];
TYTextAttribute *textAttribute = [[TYTextAttribute alloc]init];
textAttribute.color = RGBCOLOR(82, 126, 173);;
TYTextHighlight *linkTextStorage=[[TYTextHighlight alloc]init];
linkTextStorage.backgroundColor = RGBCOLOR(191, 223, 254);
linkTextStorage.userInfo=@{@"url":keywordModel.url};
linkTextStorage.backgroudInset = UIEdgeInsetsMake(1, 0, 1, 1);
[text addTextAttribute:textAttribute range:keywordModel.range];
[text addTextHighlightAttribute:linkTextStorage range:keywordModel.range];
}
//表情
for (NSInteger i=self.emotionArray.count-1;i<self.emotionArray.count;--i)
{
WBKeywordModel *keywordModel=[self.emotionArray objectAtIndex:i];
TYTextAttachment *imageStorage=[[TYTextAttachment alloc]init];
imageStorage.image=[[UIImage imageNamed:keywordModel.url] imageScaledToSize:CGSizeMake(20,20)];
imageStorage.baseline = -4;
[text replaceCharactersInRange:keywordModel.range withAttributedString:[NSAttributedString attributedStringWithAttachment:imageStorage]];
}
//url链接
for (NSInteger i=self.urlArray.count-1;i>=0;--i)
{
WBKeywordModel *keywordModel=[self.urlArray objectAtIndex:i];
NSRange range = [text.string rangeOfString:keywordModel.keyword];
if (range.length == 0) {
continue;
}
NSMutableAttributedString *att;
if (keywordModel.type==1) {
att=[[NSMutableAttributedString alloc]initWithString:@"查看图片"];
TYTextAttachment *attachment = [[TYTextAttachment alloc]init];
attachment.image = [UIImage imageNamed:@"timeline_card_small_photo_default"];
attachment.size = CGSizeMake(15, 15);
attachment.verticalAlignment = TYTextVerticalAlignmentCenter;
[att insertAttributedString:[NSAttributedString attributedStringWithAttachment:attachment] atIndex:0];
}else if (keywordModel.type==2) {
att=[[NSMutableAttributedString alloc]initWithString:@"网页链接"];
TYTextAttachment *attachment = [[TYTextAttachment alloc]init];
attachment.image = [UIImage imageNamed:@"timeline_card_small_web_default"];
attachment.size = CGSizeMake(16, 16);
attachment.baseline = -2;
[att insertAttributedString:[NSAttributedString attributedStringWithAttachment:attachment] atIndex:0];
}
TYTextAttribute *textAttribute = [[TYTextAttribute alloc]init];
textAttribute.color = RGBCOLOR(82, 126, 173);
TYTextHighlight *linkTextStorage=[[TYTextHighlight alloc]init];
linkTextStorage.backgroundColor = RGBCOLOR(191, 223, 254);
linkTextStorage.userInfo=@{@"url":keywordModel.url};
[text replaceCharactersInRange:range withAttributedString:att];
[text addTextAttribute:textAttribute range:NSMakeRange(range.location, att.length)];
[text addTextHighlightAttribute:linkTextStorage range:NSMakeRange(range.location, att.length)];
}
text.ty_characterSpacing =0;
text.ty_lineSpacing = 2;
text.ty_font = [UIFont systemFontOfSize:16];
if (flag) {
_textRender2 = [[TYTextRender alloc]initWithAttributedText:text];
_textRender2.lineBreakMode = NSLineBreakByCharWrapping;
_textRender2.highlightBackgroudRadius = 5;
_textRender2.highlightBackgroudInset = UIEdgeInsetsMake(2, 0, 2, 1);
CGFloat width = [UIScreen mainScreen].bounds.size.width-CELL_SIDEMARGIN*2;
_textRender2.size = CGSizeMake(width, [_textRender2 textSizeWithRenderWidth:width].height+2);
_contentHeight2 =_textRender2.size.height;
} else {
_textRender = [[TYTextRender alloc]initWithAttributedText:text];
_textRender.lineBreakMode = NSLineBreakByCharWrapping;
_textRender.highlightBackgroudRadius = 5;
_textRender.highlightBackgroudInset = UIEdgeInsetsMake(2, 0, 2, 1);
CGFloat width = [UIScreen mainScreen].bounds.size.width-CELL_SIDEMARGIN*2;
_textRender.size = CGSizeMake(width, [_textRender textSizeWithRenderWidth:width].height+2);
_contentHeight =_textRender.size.height;
}
}
- (void)parseAllKeywords
{
if (self.text.length>0)
{
self.atPersonArray=[WBParser keywordRangesOfAtPersonInString:self.text];
self.emotionArray=[WBParser keywordRangesOfEmotionInString:self.text];
self.urlArray=[WBParser keywordRangesOfURLInString:self.text];
self.topicArray=[WBParser keywordRangesOfSharpTrendInString:self.text];
}
}
#pragma mark - TYLabelDelegate
- (void)label:(TYLabel *)label didTappedTextHighlight:(TYTextHighlight *)textHighlight
{
}
- (void)label:(TYLabel *)label didLongPressedTextHighlight:(TYTextHighlight *)textHighlight
{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment