美文网首页iOS开发点滴
iOS话题标签#话题#NSMutableAttributedSt

iOS话题标签#话题#NSMutableAttributedSt

作者: BlueBar | 来源:发表于2022-07-07 10:03 被阅读0次

实现类似以下Label显示话题标签的样式需求,由于话题标签有边框样式,不适用富文本直接设置,所以话题边框的部分用了一个UILabel来设置,之后把话题的Label转化成UIImage,使用NSTextAttachment将image插入到NSAttributedString 中,代码如下


IMG_503DFF552012-1.jpeg

-(NSAttributedString *)topicContent{
    NSString *topicName = @"#周董先行曲#";
    NSString *msg_content = @"详细文本内容";
    NSMutableAttributedString *msg_content = [[NSMutableAttributedString alloc] initWithString:msg_content];
    
    if (topicName.length > 0) {
        UILabel *label = [[UILabel alloc]init];
        label.text = topicName;
        label.font = DEFAULT_Medium_FONT(14.f);
        label.textColor =[UIColor colorWithHexString:@"#333333"];
        label.textAlignment = NSTextAlignmentCenter;
        //计算文本宽度
        CGFloat width = [YHelper widthOfString:topicName font:label.font height:20];
        label.frame = CGRectMake(0, 0, width+15, 20);
        label.layer.backgroundColor = [UIColor whiteColor].CGColor;
        label.layer.cornerRadius = 10;
        label.layer.borderColor = [UIColor colorWithHexString:@"#909399"].CGColor;
        label.layer.borderWidth = 1;
        label.clipsToBounds = YES;
        label.layer.masksToBounds = YES;
        //拼接标签
        
        NSTextAttachment *attach = [[NSTextAttachment alloc] init];
        
        //label转图片
        
        UIImage *img = [self imageWithUIView:label];
        
        attach.image = img;
        
        attach.bounds = CGRectMake(5, -5, label.width, label.height);
        
        NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attach];
        
        [msg_content insertAttributedString:imageStr atIndex:0];
    }
    
    return msg_content;
}

//将UIView转成UIImage
- (UIImage*)imageWithUIView:(UIView*) view {
    
    UIGraphicsBeginImageContext(view.bounds.size);
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [view.layer renderInContext:ctx];
    
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return tImage;
    
}

当话题需要添加点击事件跳转的时候 UILabel就不支持添加图片点击的事件,这时候我们用YYKit中的YYLabel来设置

-(void)topicContentWith:(YYLabel *)contentLabel{
    NSString *topicName = @"#周董先行曲#";
    NSString *msg_content = @"详细文本内容";
    NSMutableAttributedString *msg_content = [[NSMutableAttributedString alloc] initWithString:content];
    [msg_content addAttribute:NSFontAttributeName value:DEFAULT_FONT(contentLabelFontSize) range:NSMakeRange(0, content.length)];
    [msg_content addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:NSMakeRange(0, content.length)];
    
    
    if (topicName.length > 0) {
        
  
        UILabel *label = [[UILabel alloc]init];
        label.text = topicName;
        label.font = DEFAULT_Medium_FONT(14.f);
        label.textColor =[UIColor colorWithHexString:@"#333333"];
        label.textAlignment = NSTextAlignmentCenter;
        CGFloat width = [YHelper widthOfString:topicName font:label.font height:20];
        label.frame = CGRectMake(0, 0, width+15, 20);
        label.layer.backgroundColor = [UIColor whiteColor].CGColor;
        label.layer.cornerRadius = 10;
        label.layer.borderColor = [UIColor colorWithHexString:@"#909399"].CGColor;
        label.layer.borderWidth = 1;
        label.clipsToBounds = YES;
        label.layer.masksToBounds = YES;
        //拼接标签
        UIImage *img = [self imageWithUIView:label];
        
        
        
        //先使用文字的方式添加点击事件
        NSRange range = NSMakeRange(0, topicName.length);
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:topicName];
       //绑定点击事件
       YYTextHighlight *highlight = [YYTextHighlight new];
       NSRange attributeRange = range;
       NSString *topicString  = [topicName substringWithRange:attributeRange];

       //linkUrl 用来在点击事件回调里取出高亮的文字
       highlight.userInfo = @{@"linkUrl":topicString};
       [attributedString setTextHighlight:highlight range:attributeRange];

        
        
        //再把点击事件的文字替换成图片  图片就是上面生成的Label图片
        //创建图片的附件
        YYTextAttachment *attach = [YYTextAttachment attachmentWithContent:img];
        attach.contentInsets = UIEdgeInsetsMake(-4, 0, 0, 0);
        //把图片附件转化为 attributedString
        NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:img contentMode:UIViewContentModeCenter attachmentSize:img.size alignToFont:[UIFont systemFontOfSize:17] alignment:YYTextVerticalAlignmentBottom];

        NSString *linkUrlString = [topicName substringWithRange:range];

        //把网址替换成 attachText 的图片附件
        [attributedString replaceCharactersInRange:range withAttributedString:attachText];

        //取出网址 绑定点击事件
        YYTextHighlight *image_linkhighlight = [YYTextHighlight new];
        NSRange image_attributeRange  = NSMakeRange(range.location, [attachText length]);
        image_linkhighlight.userInfo = @{@"linkUrl":linkUrlString};
        [attributedString setTextHighlight:image_linkhighlight range:image_attributeRange];
        
    
        [msg_content insertString:@" " atIndex:0];
        [msg_content insertAttributedString:attributedString atIndex:0];
        
    }
    
    
    //设置段落样式
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 4.0;//段内行间距

    [msg_content addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, msg_content.length)];
    contentLabel.attributedText = msg_content;
    
    //添加点击事件的回调
    @weakify(self)
    contentLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        @strongify(self)
         YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
        NSString *link = highlight.userInfo[@"linkUrl"];
        NSLog(@"%@",link);

    };

}

相关文章

  • iOS话题标签#话题#NSMutableAttributedSt

    实现类似以下Label显示话题标签的样式需求,由于话题标签有边框样式,不适用富文本直接设置,所以话题边框的部分用了...

  • 标签

    标签 标签是工具,用来用来辅助内容(描述,文章,博客,讨论等)的组织,比话题级别低;标签与话题的关系:标签可以上升...

  • iOS话题:NSOperation、NSOperationQue

    简介 NSOperation、NSOperationQueue是对GCD的一层面向对象的封装。 多线程用得多的还是...

  • 没有话题的话题

    没有话题的话题 速度快了,话题没了,可悲啊。 以前,速度很慢的时候,好像永远都有说不完的话。后来,...

  • 话题征集,话题征集!

    话题征集 亲爱的大家们! 你们一定有很多想和别人说的话吧! 没说出口的 不好意思?难以启齿? 没关系!统统讲给我们...

  • 话题:

  • 话题

    出离了愤怒 我笑一一 天地不仁呵 我又何必义 柱一支手杖 我留一个蹒跚的影 世界上所有的万物 又都了一个话题

  • 话题

    有没有一个话题 能够从开始聊到结局 以为刚开始的无话不说 到现在的恩啊嗯哦 讨厌情商这么低的自己 找不到话题

  • 话题

    慢慢来,比较快 唱歌3个月 销售2个月,半年 新工作,3个月 而这些都需要积累 写字3本字帖 写文章,天天坚持 目...

  • 话题

    忘记写了,现在开学学业比较繁忙

网友评论

    本文标题:iOS话题标签#话题#NSMutableAttributedSt

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