看图说话比较清晰,点击红色标记的区域,会展开全文。

相关知识点

  • YYLabel,truncationToken
  • NSAttributedString,YYText,YYTextHighlight

我们来看一下YYLabel的属性truncationToken,是一个富文本,当Label被截断时,该富文本显示在文末,默认与UILabel显示的一样,是三个点。

/**The truncation token string used when text is truncated. Default is nil.When the value is nil, the label use "…" as default truncation token.*/
@property (nullable, nonatomic, copy) NSAttributedString *truncationToken;

我们可以使用以下方式来指定切断文本:

YYLabel *label = [YYLabel new];
lable.text = @"我们可以使用以下方式来指定切断文本";
NSAttributedString *truncationToken = [[NSAttributedString alloc] initWithString:@"… 展开"]];
label.truncationToken = truncationToken;

接着来了解一下实现点击响应事件的YYTextHighlight 和 tapAction

/**YYTextHighlight objects are used by the NSAttributedString class clusteras the values for touchable highlight attributes (stored in the attributed stringunder the key named YYTextHighlightAttributeName).When display an attributed string in `YYLabel` or `YYTextView`, the range of highlight text can be toucheds down by users. If a range of text is turned into highlighted state, the `attributes` in `YYTextHighlight` will be used to modify (set or remove) the original attributes in the range for display.*/
@interface YYTextHighlight : NSObject <NSCoding, NSCopying>

在YYLabel或者YYTextView的富文本中,指定YYTextHighlight的范围,用户就可以在该范围内实现点击效果。

/**The tap/long press action callback defined in YYText.@param containerView The text container view (such as YYLabel/YYTextView).@param text          The whole text.@param range         The text range in `text` (if no range, the range.location is NSNotFound).@param rect          The text frame in `containerView` (if no data, the rect is CGRectNull).*/
typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);/**Tap action when user tap the highlight, default is nil.If the value is nil, YYTextView or YYLabel will ask it's delegate to handle the tap action.*/
@property (nullable, nonatomic, copy) YYTextAction tapAction;

tapAction是一个block回调,在用户点击highlight时会触发。如果没有指定tapAction, 点击会使用delegate的方式触发。

//添加点击事件YYTextHighlight *hi = [YYTextHighlight new];[text yy_setTextHighlight:hi range:[text.string rangeOfString:moreString]];hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {//这里是自己的代码};

最终的代码

- (YYLabel *)tokenLabel {if (!_tokenLabel) {_tokenLabel = [YYLabel new];_tokenLabel.frame = CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 30);_tokenLabel.numberOfLines = 0;_tokenLabel.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.75];[self addSeeMoreButtonInLabel:_tokenLabel];}return _tokenLabel;
}- (void)addSeeMoreButtonInLabel:(YYLabel *)label {UIFont *font16 = [UIFont systemFontOfSize:16];label.attributedText = [[NSAttributedString alloc] initWithString:@"我们可以使用以下方式来指定切断文本; 收起 我们可以使用以下方式来指定切断文本" attributes:@{NSFontAttributeName : font16}];NSString *moreString = @" 展开";NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"... %@", moreString]];NSRange expandRange = [text.string rangeOfString:moreString];[text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:expandRange];[text addAttribute:NSForegroundColorAttributeName value:[UIColor darkTextColor] range:NSMakeRange(0, expandRange.location)];//添加点击事件YYTextHighlight *hi = [YYTextHighlight new];[text yy_setTextHighlight:hi range:[text.string rangeOfString:moreString]];__weak typeof(self) weakSelf = self;hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {//点击展开[weakSelf setFrame:YES];};text.yy_font = font16;YYLabel *seeMore = [YYLabel new];seeMore.attributedText = text;[seeMore sizeToFit];NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentTop];label.truncationToken = truncationToken;
}- (NSAttributedString *)appendAttriStringWithFont:(UIFont *)font {if (!font) {font = [UIFont systemFontOfSize:16];}if ([_tokenLabel.attributedText.string containsString:@"收起"]) {return [[NSAttributedString alloc] initWithString:@""];}NSString *appendText = @" 收起 ";NSMutableAttributedString *append = [[NSMutableAttributedString alloc] initWithString:appendText attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : [UIColor blueColor]}];YYTextHighlight *hi = [YYTextHighlight new];[append yy_setTextHighlight:hi range:[append.string rangeOfString:appendText]];__weak typeof(self) weakSelf = self;hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {//点击收起[weakSelf setFrame:NO];};return append;
}- (void)expandString {NSMutableAttributedString *attri = [_tokenLabel.attributedText mutableCopy];[attri appendAttributedString:[self appendAttriStringWithFont:attri.yy_font]];_tokenLabel.attributedText = attri;
}- (void)packUpString {NSString *appendText = @" 收起 ";NSMutableAttributedString *attri = [_tokenLabel.attributedText mutableCopy];NSRange range = [attri.string rangeOfString:appendText options:NSBackwardsSearch];if (range.location != NSNotFound) {[attri deleteCharactersInRange:range];}_tokenLabel.attributedText = attri;
}- (void)setFrame:(BOOL)isExpand {if (isExpand) {[self expandString];self.tokenLabel.frame = CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 200);}else {[self packUpString];self.tokenLabel.frame = CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 30);}
}

如果需要用UIImage或者UIView等用作truncationToken,可以用以下方法转换成富文本。

使用这个方法可以把UIImage/UIView/CALayer转换成富文本的方式。
+ (NSMutableAttributedString *)yy_attachmentStringWithContent: contentMode:  attachmentSize: alignToFont: alignment:

如果没有把text放到YYLabel里面,而是直接赋值给truncationToken是不会有点击事件的。如下:

//添加点击事件YYTextHighlight *hi = [YYTextHighlight new];[text yy_setTextHighlight:hi range:[text.string rangeOfString:moreString]];hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {//这里是自己的代码};label.truncationToken = text;

iOS使用YYLabel 点击展开和收起全文相关推荐

  1. vue 段落文字太长(长文收缩)点击展开与收起,查看更多,收起,展开

    vue 段落文字太长点击展开与收起 文章目录 vue 段落文字太长点击展开与收起 效果截图 使用方法 组件被调用demo vue部分 属性说明 组件完整代码 效果截图 使用方法 将组件完整代码复制到你 ...

  2. uni-app中,文字超出隐藏并显示省略号(实现展开、收起全文)

    一.uni-app中,固定宽高,文字超出部分,隐藏并显示省略号. .topic_cont_text{padding: 30upx;colof: #999;background: #E1FFFF;max ...

  3. IOS 模仿qq分组那样展开与收起

    概述 模仿qq分组那样展开与收起的效果,相信大家在项目中总会碰到,今天给大家讲讲实现的思路. 不同的人有不同的实现方式,难易程度也会大不一样,我今天给大家讲的,算是一种比较简单快捷的实现方式. 内容 ...

  4. html表展开收起,HTML实现点击展开和收起

    html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD ...

  5. uni-app,文本实现展开、收起全文

    效果: 思路: 1.根据文本显示的布局中,每行大致能显示的文字个数.(实例是大致每行26个文字) 2.首先加载页面时,根据文字总长度判断是否超出自定义行数,来处理相应的数据,多余自定义行数,截取对应的 ...

  6. IOS UITableView的点击展开隐藏功能的思路

    http://www.oasku.com/?q-136.html 转载于:https://www.cnblogs.com/qiqibo/archive/2012/10/15/2725274.html

  7. div点击展开 vue_vue实现展开全部,收起全部

    1.给数组的每一项添加是否展开或收起的标志:'toggleShowDes'和长度限制的标志:'ifMore' /*获取评论列表*/ getComment: function(flag){ var m ...

  8. 【文本展开收起】uniapp—实现文本的展开与收起功能

    一.实现效果 二.代码实现 2.1 思路: 1.根据文本显示的布局中,每行大致能显示的文字个数 2.首先加载页面时,根据文字总长度判断是否超出自定义行数,来处理相应的数据,多余自定义行数,截取对应的文 ...

  9. android 评论的展开功能,Android开发实现ListView点击展开收起效果示例

    本文实例讲述了Android开发实现ListView点击展开收起效果.分享给大家供大家参考,具体如下: 废话不说先上效果: 实际上这是采用一个ExpandableListView实现的 布局文件很简单 ...

  10. vue 点击展开显示更多 点击收起部分隐藏

    vue 点击展开显示更多 点击收起部分隐藏 要求: 只展示几条数据,其余的收起.点击显示更多时候,查看全部 如下:以百度云的页面为例 直接上代码: 1.html部分: <div><d ...

最新文章

  1. centos7源码安装mysql报错_centos7.3源码安装mysql
  2. python语法大全-python基本语法
  3. 看看你能认出多少种编程语言
  4. 获取Class类对象的三种方式
  5. 基于 WPF 模块化架构下的本地化设计实践
  6. JavaFX 2 GameTutorial第3部分
  7. 推荐10款 好用的 Jquery 评分插件
  8. 汕头大学计算机英语复试,2020年汕头大学计算机应用技术考研经验分享
  9. 判断完全二叉树(顺序存储)
  10. apk java反编译_【Apk反编译】如何反编译Apk得到Java源代码
  11. Ubuntu-区域截图
  12. 佳能无线打印机服务器,佳能LBP6018W打印机WIFI无线打印心得分享
  13. 2022-08-15 第一组 顾元皓 学习笔记
  14. 那些有趣/用的Python库
  15. Matlab机器人工具箱(3-4):五自由度机械臂(计算力矩控制方法与roblocks)
  16. 5G 核心网 Inter NG-RAN node N2 based handover 信令流程
  17. anacoda里面安装包显示失败_Premiere2020安装包下载及安装教程(附pr2020配置要求)...
  18. 2022年必读的10本经管好书
  19. 2022 年 2 月产品大事记
  20. opencv光线补偿_精准光线曝光补偿和包围曝光的运用

热门文章

  1. 请问ECSHOP首页站内快讯在哪里添加和修改?
  2. js页面刷新事件 ,Javascript刷新页面的几种方法
  3. java怎么解析json_基于java解析JSON的三种方式详解
  4. [常用工具]深度学习Caffe处理工具
  5. 基于vue的UI框架ydui中的楼层跳跃scrolltab问题解决
  6. python 网盘多帐号_教你怎么拥有(很多)百度网盘2T账号
  7. Latex 求职简历模版
  8. GIT文档同步MinDoc - MinDoc模拟登陆
  9. office文档管理服务器编辑,office在线编辑方案
  10. 2019年下半年软件设计师上午真题及答案解析