美文网首页
Label实现图片加文字

Label实现图片加文字

作者: coco_CC | 来源:发表于2021-03-17 18:52 被阅读0次

实用、好用、便捷的attributedText

image.png
self.timeLabel.attributedText = [self getAttWithTitle:@"周一至周六 8:00至20:00" img:[UIImage imageNamed:@"lovecar_nearby_time"]];
/// 获取文字和图片的组合
/// @param title 文字
/// @param img 图片
- (NSAttributedString *)getAttWithTitle:(NSString *)title img:(UIImage *)img {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.headIndent = 16;
    paragraphStyle.lineSpacing = 3;
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] init];
    NSTextAttachment *imgAtt = [[NSTextAttachment alloc] init];
    imgAtt.image = img;
    imgAtt.bounds = CGRectMake(0, -4, 16, 16);
    [att appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt]];
    
    [att appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11],NSForegroundColorAttributeName : RGBA(153, 153, 153, 1)}]];
 
    [att addAttributes:@{NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, att.length)];
    return att;
}

相关文章

网友评论

      本文标题:Label实现图片加文字

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