1.给View添加UILabel
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100, 100, 200, 40);
label.backgroundColor = [UIColor yellowColor];
label.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label];
2. 图文混排
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
2.1 你好
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
[attributedText appendAttributedString:first];
2.2 图片
// 带有图片的附件对象
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"header_cry_icon"];
CGFloat lineH = label.font.lineHeight;
attachment.bounds = CGRectMake(0, - ((label.self.frame.size.height - lineH) * 0.5 - 1), lineH, lineH);
// 将附件对象包装成一个属性文字
NSAttributedString *second = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedText appendAttributedString:second];
2.3 - 马大哈
NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"马大哈"];
[attributedText appendAttributedString:third];
label.attributedText = attributedText;
3. 打印效果