阅读 113

YYLabel使用笔记,以及部分踩过的坑

本文仅记载笔者对 YYLabel相关功能的使用和踩过的一些小坑。


    1. 基本使用


    1. 使用过程遇到的小问题



1.1  简单加色修改部分字体,加事件等基本使用

NSString *operateStr = @"在你使用****前,请你务必审慎阅读、充分理解《用户协议》和《隐私政策》各条款。\n\n如你同意,请点击“我知道了”开始接受我们的服务。";NSMutableAttributedString *text = [[NSMutableAttributedString
                                        alloc]
                                       initWithString:operateStr];
    text.yy_font = TEXT_FONT(16);
    text.yy_color = HEXCOLOR(0x333333);
    NSRange canTouchRange = [operateStr rangeOfString:@"《用户协议》"];
    

    [text yy_setTextHighlightRange:canTouchRange
                             color:AppBlueColor
                   backgroundColor:[UIColor whiteColor]
                         tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                             // 点击了第一处
                         }];
    NSRange bCanTouchRange = [operateStr rangeOfString:@"《隐私政策》"];[text yy_setTextHighlightRange:bCanTouchRange
                                 color:AppBlueColor
                       backgroundColor:[UIColor whiteColor]
                             tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                 // 点击了第二处
                             }];CGSize introSize = CGSizeMake(xxxxL.frame.size.width, CGFLOAT_MAX);YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:text];// 必须重新赋一次,否则赋完attributed内容会重置对齐方式为默认self.messageL.textAlignment = NSTextAlignmentCenter;
1.2   一段文字尾部追加“全文”“展开”“收起”“详情”“更多”之类“按钮”

xxxxL.truncationToken = self.truncationToken;- (NSAttributedString *)truncationToken{
    if (!_truncationToken) {
        NSMutableAttributedString *endStr = [[NSMutableAttributedString alloc] initWithString:@"...  全文"];
        YYTextHighlight *yyh = [YYTextHighlight new];
        [yyh setColor:[UIColor greenColor]];//这个是按下的颜色
        
        @weakify(self)
        yyh.tapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            @strongify(self)
            NSLog( @"需要展开啦!!!");
        };
        
        NSRange range = [endStr.string rangeOfString:@"全文"];
        [endStr yy_setColor:HEXCOLOR(0x448bf2) range:range];
        [endStr yy_setTextHighlight:yyh range:range];
        endStr.yy_font = [UIFont systemFontOfSize:16];
        
        YYLabel *seeMore = [YYLabel new];
        seeMore.attributedText = endStr;
        [seeMore sizeToFit];
        
        self.truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:endStr.yy_font alignment:(YYTextVerticalAlignmentCenter)];
    }
    return _truncationToken;}

YYLabel虽然叫label,然而却是继承至UIView,这就导致有些和UILabel同名的属性不见得完全一样的意义。而我又有个命名习惯(nameL),label统一后缀L,后续就真把他当做了‘label’????。
此时就textAlignment属性就有区别了,创建YYLabel对象时设置为 NSTextAlignmentCenter ,而显示时候却还是left。如以下场景:


设计样式

代码执行结果


查找半天无果,最后发现 在给label赋textLayout或attributedText之后需要重新赋一次对齐方式:

xxxxL.attributedText = text;
// xxxxL.textLayout = layout;
xxxxL.textAlignment = NSTextAlignmentCenter;

另外,numbeOfLines属性也有次情况,例如根据不同情况对行数进行不同限制也要如此操作:

xxxxL.attributedText = text;
// xxxxL.textLayout = layout;
xxxxL.numberOfLines = 5;



作者:boyka_yang
链接:https://www.jianshu.com/p/1a7725df78e0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐