美文网首页
yylabel添加高亮点击字体

yylabel添加高亮点击字体

作者: child_cool | 来源:发表于2017-08-15 14:43 被阅读483次
        NSMutableAttributedString *muAtt = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",model.username,model.entitytypename,model.entityname]];
        
        [muAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, muAtt.length)];
        
        [muAtt setTextHighlightRange:NSMakeRange(0, model.username.length)
                             color:HEXCOLOR(0x1cb177)
                   backgroundColor:self.titleLabel.backgroundColor
                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
                             NSLog(@"%@",[NSString stringWithFormat:@"Tap: %@", [text.string substringWithRange:range]]);
                         }];
        
        [muAtt setTextHighlightRange:NSMakeRange(muAtt.length - model.entityname.length, model.entityname.length)
                             color:HEXCOLOR(0x1cb177)
                   backgroundColor:self.titleLabel.backgroundColor                     tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
                             NSLog(@"%@",[NSString stringWithFormat:@"Tap: %@", [text.string substringWithRange:range]]);
                         }];
    
    
        
        self.titleLabel.attributedText = muAtt;
    //    self.titleLabel.preferredMaxLayoutWidth = SCREEN_WIDTH -32;
    
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 32, MAXFLOAT);
        
        //计算文本尺寸
        YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:muAtt];
        self.titleLabel.textLayout = layout;
    
    

    相关文章

      网友评论

          本文标题:yylabel添加高亮点击字体

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