NSTextAttachment图文混排

UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, 300, 21)];
//富文本
NSString *message = @"我是郝高明,外号小胖,哈哈~";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];

NSTextAttachment *attachment = [[NSTextAttachment alloc]initWithData:nil ofType:nil];  
UIImage *image = [UIImage imageNamed:@"80.png"];  
attachment.image = image;  
attachment.bounds = CGRectMake(0, 0, 20, 20);  
  
NSAttributedString *text = [NSAttributedString attributedStringWithAttachment:attachment];  
[str insertAttributedString:text atIndex:5];  
  
lable.attributedText = str;  

你可能感兴趣的:(NSTextAttachment图文混排)