YYLabel 点击展开全文

作者: 34码的小孩子 | 来源:发表于2018-12-03 11:35 被阅读140次

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


折叠效果图
展开效果图

相关知识点

  • 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 cluster
 as the values for touchable highlight attributes (stored in the attributed string
 under 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) {
      //这里是自己的代码
    };

最终的代码

- (void)addSeeMoreButton {
    YYLabel *label = [YYLabel new];
    lable.attributeText = [[NSAttributedString alloc] initWithString:@"我们可以使用以下方式来指定切断文本"]];

    UIImage *image = [UIImage imageNamed:@"qa_expandimage"];
    NSString *moreString = @" 展开";
    UIFont *font16 = [UIFont systemFontOfSize:16];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"... %@", moreString]];
    NSRange expandRange = [text.string rangeOfString:moreString];

    [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:kAPPMainColor] range:expandRange];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:kDefaultTextColorNoData] range:NSMakeRange(0, expandRange.location)];
    
    //添加点击事件
    YYTextHighlight *hi = [YYTextHighlight new];
    [text yy_setTextHighlight:hi range:[text.string rangeOfString:moreString]];
    
   
    hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
       //这里是自己的代码
    };
    
    text.yy_font = font16;
    
    NSAttributedString *imageAttri = [NSAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentTop];
    [text insertAttributedString:imageAttri atIndex:expandRange.location];
    
    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;
}

上面的代码中,使用以下方法把UIImage 和 YYLabel转换成富文本。

使用这个方法可以把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;

相关文章

  • YYLabel 点击展开全文

    看图说话比较清晰,点击红色标记的区域,会展开全文。 相关知识点 YYLabel,truncationToken N...

  • YYLabel简单使用

    最近尝试研究了一下YYLabel,学习了一下YYLabel文本响应点击及YYLabel换行功能,下面介绍一下我的收...

  • YYKit框架学习之YYLabel

    一、YYLabel 强大的富文本显示功能,可根据文字的range随意添加点击事件 1 自动换行 YYLabel *...

  • YYText 与 NSMutableAttributedStri

    1、字符串部分高亮➕点击事件 - (YYLabel*)createFooterLabelWithHeadStrin...

  • YYLabel 图片消失坑

    点击文本跳转到其他界面,突然发现YYLabel控件上的图片消失了 解决办法:重新赋值给YYLabel控件attri...

  • UILabel展开和收起(YYLabel)

    原理:1.设置数据时,算出显示的最大行高度,和真实打开后的高度2.多行尾部的展开,通过YYLabel的trunca...

  • 嘴巴赋

    阅读技巧:识别二维码,打开文章,拉到下面“阅读全文”,点击进去看到“温馨提示”,不要“确定”,点击“取消”,展开全...

  • 什么是“道德”?

    阅读技巧:识别二维码,打开文章,拉到下面“阅读全文”,点击进去看到“温馨提示”,不要“确定”,点击“取消”,展开全...

  • 谦卑的力量(深度好文)

    阅读技巧:识别二维码,打开文章,拉到下面“阅读全文”,点击进去看到“温馨提示”,不要“确定”,点击“取消”,展开全...

  • 身心健康 幸福快乐

    阅读技巧:识别二维码,打开文章,拉到下面“阅读全文”,点击进去看到“温馨提示”,不要“确定”,点击“取消”,展开全...

网友评论

    本文标题:YYLabel 点击展开全文

    本文链接:https://www.haomeiwen.com/subject/vrwecqtx.html