NSAttributedString
UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *content = [[NSMutableAttributedString alloc] init];
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"hello" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
[content appendAttributedString:first];
//创建附件
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
//附件添加图片
attachment.image = [UIImage imageNamed:@"header_cry_icon"];
attachment.bounds = CGRectMake(0, 0, label.font.lineHeight, label.font.lineHeight);
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
//加入富文本中
[content appendAttributedString:attachmentString];
NSAttributedString *thired = [[NSAttributedString alloc] initWithString:@"你好" attributes:@{NSForegroundColorAttributeName:[UIColor purpleColor]}];
[content appendAttributedString:thired];
label.attributedText = content;
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
网友评论