美文网首页
YYText 与 NSMutableAttributedStri

YYText 与 NSMutableAttributedStri

作者: fcf7850dd33e | 来源:发表于2021-04-15 16:30 被阅读0次

    1、字符串部分高亮➕点击事件

    - (YYLabel*)createFooterLabelWithHeadString:(NSString*)headStringcontainString:(NSString*)containStringactionBlock:(void(^)())block {

        YYLabel*footerLabel = [[YYLabelalloc]init];

        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:headString];

        footerLabel.font=Font(12);

        attri.yy_color = LightGrayColor;

        if([containStringisValidString]) {

            [attriyy_setTextHighlightRange:[headStringrangeOfString:containString]color:HighlightColorbackgroundColor:ColorCleartapAction:^(UIView*_NonnullcontainerView,NSAttributedString*_Nonnulltext,NSRangerange,CGRectrect) {

                if(block) {

                    block();

                }

            }];

        }

        footerLabel.attributedText= attri;

        footerLabel.numberOfLines=0;

        // 设置最大宽度

        footerLabel.preferredMaxLayoutWidth=MainScreenWidth-18;

        footerLabel.textAlignment=NSTextAlignmentLeft;

        returnfooterLabel;

    }

    2、字符串部分高亮

            NSMutableAttributedString *attributeStr1 = [[NSMutableAttributedString alloc] initWithString:subMessage];

            attributeStr1.yy_font=Font(16);

            attributeStr1.yy_color=RGBHex(@"2A2A2A");

            [attributeStr1yy_setColor:RGBHex(@"F85E56")range:[subMessagerangeOfString:@""]];

           subMessageLabel.attributedText= attributeStr1;

    相关文章

      网友评论

          本文标题:YYText 与 NSMutableAttributedStri

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