iosiOS_UIKit

NSTextAttachment图文混排

2018-05-10  本文已影响0人  絮语时光杨

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;  
上一篇 下一篇

猜你喜欢

热点阅读