YYText 是一个强大的富文本库.在iOS开发中经常会用到富文本。我们常用到的效果如下图所示:

yytext.png

下面我们来看看各个功能的实现:
先创建一个可变属性字符串:

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:DaoXiang];[text yy_setFont:[UIFont systemFontOfSize:20] range:text.yy_rangeOfAll];//字体text.yy_lineSpacing = 20;//行间距

DaoXiang是一个宏定义字符串,

DaoXiang.png

字体、颜色、文字间距


实现:

NSRange range0 = [[text string] rangeOfString:@"对这个世界如果你有太多的抱怨" options:NSCaseInsensitiveSearch];
//字体
[text yy_setFont:[UIFont systemFontOfSize:25] range:range0];
//文字颜色
[text yy_setColor:[UIColor purpleColor] range:range0];
//文字间距
[text yy_setKern:@(2) range:range0];

效果:

字体、颜色、文字间距.png

文字描边(空心字)


实现代码

NSRange range1 = [[text string] rangeOfString:@"跌倒了 就不敢继续往前走" options:NSCaseInsensitiveSearch];
//文字描边(空心字)默认黑色,必须设置width
[text yy_setStrokeColor:[UIColor orangeColor] range:range1];
[text yy_setStrokeWidth:@(2) range:range1];

效果

文字描边

删除样式、下划线


实现代码

NSRange range2 = [[text string] rangeOfString:@"为什麼 人要这麼的脆弱 堕落" options:NSCaseInsensitiveSearch];YYTextDecoration *decoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSinglewidth:@(1)color:[UIColor blueColor]];
//删除样式
[text yy_setTextStrikethrough:decoration range:range2];
//下划线
[text yy_setTextUnderline:decoration range:range2];

效果

删除样式、下划线

边框


实现代码

NSRange range3 = [[text string] rangeOfString:@"请你打开电视看看 多少人" options:NSCaseInsensitiveSearch];//边框
YYTextBorder *border = [YYTextBorder new];
border.strokeColor = [UIColor colorWithRed:1.000 green:0.029 blue:0.651 alpha:1.000];
border.strokeWidth = 3;
border.lineStyle = YYTextLineStylePatternCircleDot;
border.cornerRadius = 3;
border.insets = UIEdgeInsetsMake(0, -2, 0, -2);[text yy_setTextBorder:border range:range3];

效果

边框

阴影


实现代码

NSRange range4 = [[text string] rangeOfString:@"为生命在努力勇敢的走下去" options:NSCaseInsensitiveSearch];//阴影
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[UIColor redColor]];
[shadow setShadowBlurRadius:1.0];
[shadow setShadowOffset:CGSizeMake(2, 2)];
[text yy_setShadow:shadow range:range4];

效果

阴影

文本内阴影


实现代码

NSRange range5 = [[text string] rangeOfString:@"我们是不是该知足" options:NSCaseInsensitiveSearch];//文本内阴影
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor redColor];
shadow.offset = CGSizeMake(0, 2);
shadow.radius = 1;
[text yy_setTextInnerShadow:shadow range:range5];

效果

文本内阴影

多重阴影


实现代码

//多重阴影
NSRange range6 = [[text string] rangeOfString:@"珍惜一切就算没有拥有" options:NSCaseInsensitiveSearch];YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor redColor];
shadow.offset = CGSizeMake(0, -1);
shadow.radius = 1.5;
YYTextShadow *subShadow = [YYTextShadow new];
subShadow.color = [UIColor greenColor];
subShadow.offset = CGSizeMake(0, 1);
subShadow.radius = 1.5;
shadow.subShadow = subShadow;
[text yy_setTextShadow:shadow range:range6];YYTextShadow *shadow1 = [YYTextShadow new];
shadow1.color = [UIColor orangeColor];
shadow1.offset = CGSizeMake(0, 2);
shadow1.radius = 1;
[text yy_setTextInnerShadow:shadow range:range6];

效果

多重阴影

简单文本高亮


实现代码

//文本高亮简单版
NSRange range8 = [[text string] rangeOfString:@"随著稻香河流继续奔跑" options:NSCaseInsensitiveSearch];
[text yy_setTextHighlightRange:range8color:[UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000]backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){[AppUtility showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];}];

效果

简单文本高亮

//文本高亮pro


实现代码

//文本高亮pro
UIColor *colorNormal = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
UIColor *colorHighlight = [UIColor purpleColor];NSRange range9 = [[text string] rangeOfString:@"微微笑 小时候的梦我知道" options:NSCaseInsensitiveSearch];YYTextDecoration *decorationNomal = [YYTextDecoration decorationWithStyle:YYTextLineStyleSinglewidth:@(1)color:colorNormal];
YYTextDecoration *decorationHighlight = [YYTextDecoration decorationWithStyle:YYTextLineStyleSinglewidth:@(1)color:colorHighlight];
//未点击时颜色
[text yy_setColor:colorNormal range:range9];
//未点击时下划线
[text yy_setTextUnderline:decorationNomal range:range9];//点击后的状态
YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setColor:colorHighlight];
[highlight setUnderline:decorationHighlight];
highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {[AppUtility showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
};
[text yy_setTextHighlight:highlight range:range9];

效果

文本高亮pro

@,#,email,link


实现代码


// 高亮状态的背景
YYTextBorder *highlightBorder = [YYTextBorder new];
highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
highlightBorder.cornerRadius = 3;
highlightBorder.fillColor = [UIColor greenColor];//@用户名称
NSArray *resultAt= [[Utility regexAt] matchesInString:text.string options:kNilOptions range:text.yy_rangeOfAll];for (NSTextCheckingResult *at in resultAt)
{if (at.range.location == NSNotFound && at.range.length <= 1){continue;}if ([text yy_attribute:YYTextHighlightAttributeName atIndex:at.range.location] == nil){[text yy_setColor:[UIColor blueColor] range:at.range];// 高亮状态YYTextHighlight *highlight = [YYTextHighlight new];[highlight setBackgroundBorder:highlightBorder];// 数据信息,用于稍后用户点击NSString *atName = [text.string substringWithRange:NSMakeRange(at.range.location + 1, at.range.length - 1)];highlight.userInfo = @{@"linkValue" : atName,@"linkType":@(LinkTypeAt)};[text yy_setTextHighlight:highlight range:at.range];}
}//#话题#
NSArray *resultTopic = [[Utility regexTopic] matchesInString:text.string options:kNilOptions range:text.yy_rangeOfAll];for (NSTextCheckingResult *at in resultTopic)
{if (at.range.location == NSNotFound && at.range.length <= 1){continue;}if ([text yy_attribute:YYTextHighlightAttributeName atIndex:at.range.location] == nil){[text yy_setColor:[UIColor blueColor] range:at.range];// 高亮状态YYTextHighlight *highlight = [YYTextHighlight new];[highlight setBackgroundBorder:highlightBorder];// 数据信息,用于稍后用户点击highlight.userInfo = @{@"linkValue" : [text.string substringWithRange:NSMakeRange(at.range.location, at.range.length)],@"linkType":@(LinkTypeTopic)};[text yy_setTextHighlight:highlight range:at.range];}
}//email
NSArray *resultEmail = [[Utility regexEmail] matchesInString:text.string options:kNilOptions range:text.yy_rangeOfAll];for (NSTextCheckingResult *at in resultEmail)
{if (at.range.location == NSNotFound && at.range.length <= 1){continue;}if ([text yy_attribute:YYTextHighlightAttributeName atIndex:at.range.location] == nil){[text yy_setColor:[UIColor blueColor] range:at.range];// 高亮状态YYTextHighlight *highlight = [YYTextHighlight new];[highlight setBackgroundBorder:highlightBorder];// 数据信息,用于稍后用户点击highlight.userInfo = @{@"linkValue" : [text.string substringWithRange:NSMakeRange(at.range.location, at.range.length)],@"linkType":@(LinkTypeEmail)};[text yy_setTextHighlight:highlight range:at.range];}
}//link
NSArray *resultLink = [[Utility regexUrl] matchesInString:text.string options:kNilOptions range:text.yy_rangeOfAll];for (NSTextCheckingResult *at in resultLink)
{if (at.range.location == NSNotFound && at.range.length <= 1){continue;}if ([text yy_attribute:YYTextHighlightAttributeName atIndex:at.range.location] == nil){[text yy_setColor:[UIColor blueColor] range:at.range];// 高亮状态YYTextHighlight *highlight = [YYTextHighlight new];[highlight setBackgroundBorder:highlightBorder];// 数据信息,用于稍后用户点击highlight.userInfo = @{@"linkValue" : [text.string substringWithRange:NSMakeRange(at.range.location, at.range.length)],@"linkType":@(LinkTypeURL)};[text yy_setTextHighlight:highlight range:at.range];}
}

效果

@,#,email,link

以话题为例,简单说明一下:首先用正则来查出所有话题,然后再将话题部分设置高亮状态以及数据信息,这些信息在用户点击时会用到。后面会说到如何处理点击,别急。

添加gif动画

实现代码

YYImage *image = [YYImage imageNamed:@"zuqiu"];
image.preloadAllAnimatedImageFrames = YES;
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
imageView.autoPlayAnimatedImage = NO;
[imageView startAnimating];NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:[UIFont systemFontOfSize:16] alignment:YYTextVerticalAlignmentBottom];
[text appendAttributedString:attachText];

添加普通图片直接使用UIImage和UIImageView就可以了。

布局


实现代码

CGSize size = CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];// 获取文本显示位置和大小
//layout.textBoundingRect; // get bounding rect
//layout.textBoundingSize; // get bounding size

可以由YYTextLayout获取文本的bonding rectsize

YYLabel添加


实现代码

YYLabel *label = [YYLabel new];
label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {if ([self.clickDelegate respondsToSelector:@selector(label:tapHighlight:inRange:)]){YYTextHighlight *highlight = [text yy_attribute:YYTextHighlightAttributeName atIndex:range.location];[self.clickDelegate label:(YYLabel *)containerView tapHighlight:highlight inRange:range];}
};
label.frame = CGRectMake(0, 0, SCREEN_WIDTH, layout.textBoundingSize.height);
label.textAlignment = NSTextAlignmentCenter;
label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
label.numberOfLines = 0;
label.backgroundColor = RGBCOLOR(246, 246, 246);
label.textLayout = layout;
[self addSubview:label];
```
这里有个属性`highlightTapAction`就是用来处理点击高亮文字事件的,在这里,我定义了一个delegate:
```
@protocol YYHiglightTextClickDelegate <NSObject>- (void)label:(YYLabel *)labeltapHighlight:(YYTextHighlight *)highlightinRange:(NSRange)textRange;@end
```只要实现这个delegate就能方便的处理点击各种高亮文字的事件。`YYTextHighlight `里面包含了一个userInfo,包含了很多需要处理的信息,通过它,能够很容易的处理点击事件,我这里在UIViewController中做了一个实现:
```
#pragma mark - YYHiglightTextClickDelegate
- (void)label:(YYLabel *)labeltapHighlight:(YYTextHighlight *)highlightinRange:(NSRange)textRange
{NSDictionary *info = highlight.userInfo;LinkType linkType = [info[@"linkType"] integerValue];NSString *linkValue = info[@"linkValue"];switch (linkType) {case LinkTypeAt:{[AppUtility showMessage:[NSString stringWithFormat:@"选中at:%@",linkValue]];}break;case LinkTypeTopic:{[AppUtility showMessage:[NSString stringWithFormat:@"选中话题:%@",linkValue]];}break;case LinkTypeEmail:{[AppUtility showMessage:[NSString stringWithFormat:@"选中email:%@",linkValue]];}break;case LinkTypeURL:{[AppUtility showMessage:[NSString stringWithFormat:@"选中url:%@",linkValue]];}break;case LinkTypePhoneNum:{[AppUtility showMessage:[NSString stringWithFormat:@"选中phone:%@",linkValue]];}break;default:break;}
}
```
我在userInfo中传入了两对键值:
####表情
---
实现代码
```
NSMutableDictionary *mapper = [NSMutableDictionary new];
mapper[@":smile:"] = [self imageWithName:@"002"];
mapper[@":cool:"] = [self imageWithName:@"013"];
mapper[@":biggrin:"] = [self imageWithName:@"047"];
mapper[@":arrow:"] = [self imageWithName:@"007"];
mapper[@":confused:"] = [self imageWithName:@"041"];
mapper[@":cry:"] = [self imageWithName:@"010"];
mapper[@":wink:"] = [self imageWithName:@"085"];
mapper[@":zuqiu:"] = [self imageWithName:@"zuqiu"];YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
parser.emoticonMapper = mapper;YYTextLinePositionSimpleModifier *mod = [YYTextLinePositionSimpleModifier new];
mod.fixedLineHeight = 22;
```
`YYLabel`已经实现了一个简单的表情解析器`YYTextSimpleEmoticonParser `,你只需要设置一下映射器`emoticonMapper`就好.然后把解析器和modifier传给`YYLabel`.
最后将文本传给`attributedText`
```
label.textParser = parser;
label.linePositionModifier = mod;
label.attributedText = text;
```
`linePositionModifier`是在文本发生变化时才需要的属性,一般YYTextView用的多,比如修改一个文本之后,整个文本发生了变化,就需要这个属性值。表情资源来自YYText,可以下载源码获得。另外我写了个小[demo](https://github.com/flowyears/AttributeTextDemo),包含YYLabel,YYTextView以及TTTAttributedLabel的简单实用。[TTTAttributedLabel简单使用](http://www.jianshu.com/p/b457a49fac3d)简单的描述了TTTAttributedLabel的使用,并且在最后将YYLabel,YYTextView以及TTTAttributedLabel三者的功能做了一个简单的比较。在学习的过程中发现有几个属性无效,一个是斜体,一个是文字背景色,如果有知道怎么实现的小伙伴请告诉我一下,谢谢。以上只是一个简单的使用。这里有一篇更深层次的剖析:[YYKit之YYText](http://www.cnblogs.com/lujianwenance/p/5716804.html).end.

作者:0o冻僵的企鹅o0
链接:https://www.jianshu.com/p/60aee32ade55
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

YYText 库学习总结相关推荐

  1. 【libevent】libevent库学习总结(一)——基础

    libevent库学习总结(一)--基础 一.基础 1.1. 介绍 Libevent是一个用于开发可伸缩网络服务器的事件通知库.Libevent API提供了一种机制来执行回调函数,当某个特定事件发生 ...

  2. Guava库学习:学习Guava EventBus(二)EventBus 事件订阅示例

    2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Guava库学习:学习Guava EventBus(二)EventBus 事件订阅示例 上一篇Guava库学习:学习 ...

  3. POCO C++库学习和分析

    POCO C++库学习和分析 -- 序 1. POCO库概述: POCO是一个C++的开源库集.同一般的C++库相比,POCO的特点是提供了整一个应用框架.如果要做C++程序应用框架的快速开发,我觉得 ...

  4. muduo网络库学习(八)事件驱动循环线程池EventLoopThreadPool

    muduo是支持多线程的网络库,在muduo网络库学习(七)用于创建服务器的类TcpServer中也提及了TcpServer中有一个事件驱动循环线程池,线程池中存在大量线程,每个线程运行一个Event ...

  5. muduo网络库学习(七)用于创建服务器的类TcpServer

    目前为止,涉及到的绝大多数操作都没有提及线程,EventLoop,Poller,Channel,Acceptor,TcpConnection,这些对象的执行都是在单独线程完成,并没有设计多线程的创建销 ...

  6. muduo网络库学习(四)事件驱动循环EventLoop

    muduo的设计采用高并发服务器框架中的one loop per thread模式,即一个线程一个事件循环. 这里的loop,其实就是muduo中的EventLoop,所以到目前为止,不管是Polle ...

  7. python xlwings 切片_Python xlwings库学习笔记(1)

    Python xlwings库学习笔记(1) Python是最近几年很火的编程语言,被办公自动化的宣传吸引入坑,办公自动化必然绕不开Excel的操作,能操作Excel的库有很多,例如: xlrd xl ...

  8. python标准库学习笔记

    原创:python标准库学习笔记 数据结构 bisect 模块里实现了一个向列表插入元素时也会顺便排序的算法. struct - 二进制数据结构:用途:在 Python 基本数据类型和二进制数据之间进 ...

  9. requests库学习

    requests库学习 requests快速上手 http://2.python-requests.org/zh_CN/latest/user/quickstart.html Requests库是用来 ...

  10. Guava库学习:学习Concurrency(九)RateLimiter

    2019独角兽企业重金招聘Python工程师标准>>> 链接地址:http://www.xx566.com/detail/164.html 上一篇,Guava库学习:学习Concur ...

最新文章

  1. 解决LaTex中插入Visio画图有多余边框的问题
  2. android 获取url中的参数
  3. MySQL数据库创建及删除操作
  4. 原来Python破解受密码保护的zip文件这么简单,不保证一定成功
  5. ERROR 1045 (28000): Access denied for user root@localhost (using password:
  6. windows副本不是正版怎么办_盗版系统总是崩溃?别着急,让我来告诉你正版系统怎么下载...
  7. python使用virtualenv在本地新建虚拟环境
  8. bzoj 1026: [SCOI2009]windy数
  9. fancyupload java_javascript-如何在内部Windows身份验证Intranet上...
  10. 硅谷程序员跳槽排行榜:最爱竟不是 Google 苹果 Facebook!
  11. 【C++笔记】运算符重载
  12. 利用R语言做可重复性报告研究
  13. 【项目实战——emos在线办公系统】:会议申请、请假申请等部分代码理解
  14. ROS配置ipv6方法
  15. ChromeOS+Win双系统安装教程
  16. excel小技巧之如何提取指定字符之前的字段
  17. 产品经理必修课(4):深挖需求
  18. vue3 + tsx + pinia + jest开发模板整理
  19. 重生之丹王,炼丹入门
  20. 51单片机 (四)延时函数

热门文章

  1. 浏览器兼容性问题解决方案 · 总结
  2. 51单片机通过WIFI模块ESP8266控制LED灯(大致内容,经过测试成功实现)
  3. 初识HarmonyOS—小熊派BearPi-HM_Nano上手指南第二篇:源码获取
  4. Matlab数理统计工具箱
  5. Teorex Inpaint 6.0 简体中文最新破解版(自动去除图片上的水印)
  6. retainAll()和removeAll()
  7. 【图像分割】基于马尔可夫随机场实现图像分割附matlab代码
  8. 线性系统的的串联校正
  9. 史上最全人工智能英文原版PDF教材1.03G资源包Artificial Intelligence
  10. python openpose