iOS使用YYLabel 点击展开和收起全文

article/2025/8/28 4:58:32

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

 

相关知识点

  • 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;

 

 


http://chatgpt.dhexx.cn/article/gKO8pbiT.shtml

相关文章

java爬取新浪微博带有“展开全文”的完整微博文本

获取新浪微博“展开全文”的完整文本 在个人主页的响应中&#xff0c;这篇微博的表示形式是这样的&#xff1a; <div class\"WB_text W_f14\" node-type\"feed_list_content\" nick-name\"Vista看天下\">\n 【一堂课…

uni-app,一段文字实现展开、收起全文点点点

效果&#xff1a; 思路&#xff1a; 1.根据文本显示的布局中&#xff0c;每行大致能显示的文字个数。&#xff08;实例是大致每行26个文字&#xff09; 2.首先加载页面时&#xff0c;根据文字总长度判断是否超出自定义行数&#xff0c;来处理相应的数据&#xff0c;多余自定义…

CSDN阅读全文自动展开插件,安排上!

TJ平时经常利用一些碎片时间逛逛CSDN&#xff0c;由于是碎片时间&#xff0c;往往都是看到哪是哪&#xff0c;所以也没有登录&#xff0c;于是会碰到一个情况&#xff0c;就是看到一篇文章觉得不错&#xff0c;刚看了两句就让点击展开全文&#xff0c;点击之后还要求登录才行&a…

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

一、uni-app中&#xff0c;固定宽高&#xff0c;文字超出部分&#xff0c;隐藏并显示省略号。 .topic_cont_text{padding: 30upx;colof: #999;background: #E1FFFF;max-height: 130upx;overflow: hidden;word-break: break-all; /* break-all(允许在单词内换行。) */text-ov…

iOS文本展开收起,使用YYKit展开全文和收起全文,支持图文混排

使用YYKit展开全文和收起全文&#xff0c;支持图文混排 使用简单 1.依赖库&#xff1a; 该工具是基于YYKit封装的工具类&#xff0c; 使用前先用cocoapods导入依赖库&#xff1a;pod ‘YYKit’ 2.使用方法 导入头文件 #import “YYLabel_gcz.h” 初始化和赋值内容 YYLabel…

前端页面 div+css内容太长,实现点击展开余下全文(修改版)

<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <title>文章高度展开</title> <style> .content{padding:10px 12px 48px;font-size:18px;color:#2b2b2b;line-height:1.7em;height:300px; /*初…

[javascript] 实现展开全文和收起全文效果

在展示大量文本的时候,很多网站会在页面上出现一个展开全文的文字按钮 , 点击这个按钮才会展开全部内容 . 使用jquery比较容易实现 , 最开始我想直接根据vuejs的语法来实现效果 , 结果失败 , 还是jq做起来简单 思路是 , 获取当前文本的div高度 ,超过一定高度就增加一个class值 …

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

效果: 思路&#xff1a; 1.根据文本显示的布局中&#xff0c;每行大致能显示的文字个数。&#xff08;实例是大致每行26个文字&#xff09; 2.首先加载页面时&#xff0c;根据文字总长度判断是否超出自定义行数&#xff0c;来处理相应的数据&#xff0c;多余自定义行数&#…

[HTML+CSS+Vue.js] 超长文本等内容默认折叠显示,点击展开全文,再点击收起(仿知乎效果)

今天在做一个仿博客主页&#xff0c;日记的部分想做成折叠展开的效果&#xff0c;这样比较有利于浏览和交互&#xff0c;然后想起知乎问答的页面效果&#xff0c;就很符合我想要的样式&#xff1a; 因为之前没做过&#xff0c;不知从何下手&#xff0c;在网上查了大半天&#…

vue 展开全文,收起全文

样式效果 1.展开全文 2.收起全文 html <div class"content_box"><div class"cont_top"><div class"bluebox"></div><span>详情描述</span><span class"divider"></span></div&g…

html如何制作展开全文,如何实现文章内容页点击“展开阅读全文”的功能

我们在手机端看CSDN或知乎上的文章时&#xff0c;都会有一个点击展开阅读全文的按钮。这个功能效果使用还是很广泛的&#xff0c;网上也有很多这样的jq插件可以实现&#xff0c;今天小郭给大家分享一下自己在项目中常用到的js点击展开阅读全文的代码。 如下图&#xff1a; 完整…

令人头疼的“展开原文”,请收好这款神器!

互联网时代下&#xff0c;网上阅读资讯也早已成为用户习惯。 但近几年&#xff0c;随着互联网的发展&#xff0c;一个令人头疼的现象出现了&#xff0c;即网上越来越多文章&#xff0c;都需要点击一次&#xff0c;才能查看全文了&#xff0c;就像下面这样子…… 尤其对于网站的…

百度白皮书5.0解读如何合理设置展开全文功能

导读&#xff1a;2020年3月&#xff0c;百度发布了《百度APP移动搜索落地页体验白皮书5.0》。白皮书5.0发布后&#xff0c;收到许多开发者关于体验规范的反馈建议&#xff0c;针对开发者集中关心的规范问题&#xff0c;百度搜索将推出白皮书5.0系列解读文章&#xff0c;精准助力…

Mac快捷输入iPhone短信验证码

一、验证码输入&#xff1a; 二、设置步骤&#xff1a; iPhone&#xff1a;设置->短信->短信转发->打开 Mac开关

Web功能设计:验证码

验证码 简介实现简介 验证码(CAPTCHA——Completely Automated Public Turing test to tell Computers and Humans Apart,全自动区分计算机和人类的图灵测试),是一种区分用户是计算机还是人的公共全自动程序。作用:防止恶意破解密码、刷票、论坛灌水或某个黑客对某一个特定…

输入验证码投票功能的实现

点击投票的时候判断投票的时间是否在这个活动时间内 获取当前时间 传到后台和活动时间判断 投票的时间是否在这个活动时间之内 如果在的 话 弹出验证码 输入验证码投票 中间插一句验证码功能 前台 后台 生成和校验 在验证码有误之后 重新调用验证码方法刷新 验证码成…

登录时候输入验证码,验证码图片从服务器获取方法

登录时候输入验证码&#xff0c;验证码图片从服务器获取方法 小验证码图片 源码分享&#xff1a;http://pan.baidu.com/s/1skK7jRJ 展示效果&#xff1a; 登录时候输入验证码&#xff0c;验证码图片从服务器获取方法 - wangyue.123.com - moonstak jsp页面&#xff1a; <…

iOS 分割输入验证码的视觉效果

通常情况下&#xff0c;在用户注册或者登录的时候我们会用到手机验证码&#xff0c;而有时就会需要&#xff0c;分割输入验证码的视觉效果。 那这种情况&#xff0c;我们怎么实现呢&#xff1f; 在网上查了很多&#xff0c;好多都只是给了实现代码&#xff0c;给的东西都不是很…

input输入框模拟验证码输入效果

今天看到一个帖子&#xff0c;说到用input输入框模拟滴滴、摩拜等app验证码输入效果&#xff0c;提到了一个方案&#xff1a; 1、利用input来获得焦点&#xff0c;自动调用手机的数字键盘 2、实际将输入框用透明度隐藏 3、用label的for属性与input联动来显示输入的数字 于是…

vue实现验证码输入

需求&#xff1a;这种样式的验证码输入&#xff0c;进入页面时光标停留在第一个格子&#xff0c;随着输入的数字向后移动。 实现逻辑&#xff1a; ①首先考虑样式&#xff0c;将六个格子及获取光标的样式写出来。 ②需要一个input框作为输入&#xff0c;所以在页面上加input框&…