最近对图文混排的技术点做了点研究,希望能帮助开发中遇到困难的人!废话不多说直接看效果图
1.0
2.0
核心代码:
-(void)setModel:(ZHModel *)model{
_model = model;
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:model.text];
if (![model.icon isEqualToString:@"123"]) {
NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:@"\t"];
[str insertAttributedString:str1 atIndex:0];
NSMutableParagraphStyle *pr = [[NSMutableParagraphStyle alloc] init];
pr.lineBreakMode = NSLineBreakByTruncatingTail;
pr.alignment = NSTextAlignmentLeft;
pr.lineSpacing = 3;
pr.paragraphSpacing = 0;
NSDictionary
*dict = @{NSFontAttributeName:[UIFont systemFontOfSize:14
weight:8],NSForegroundColorAttributeName:[UIColor
greenColor],NSParagraphStyleAttributeName:pr};
[str setAttributes:dict range:NSMakeRange(0, str.length)];
NSAttributedString *str2 = [[NSAttributedString alloc] initWithString:@"\n\t"];
[str appendAttributedString:str2];
NSTextAttachment *att = [[NSTextAttachment alloc] init];
att.image = [UIImage imageNamed:model.icon];
att.bounds = CGRectMake(0, 0, 300, 80);
[str appendAttributedString:[NSAttributedString attributedStringWithAttachment:att]];
self.label1.attributedText = str;
}else{
NSMutableParagraphStyle *pr = [[NSMutableParagraphStyle alloc] init];
pr.lineBreakMode = NSLineBreakByTruncatingTail;
pr.alignment = NSTextAlignmentCenter;
pr.lineSpacing = 3;
pr.paragraphSpacing = 0;
NSDictionary
*dict = @{NSFontAttributeName:[UIFont systemFontOfSize:14
weight:8],NSForegroundColorAttributeName:[UIColor
greenColor],NSParagraphStyleAttributeName:pr};
[str setAttributes:dict range:NSMakeRange(0, str.length)];
self.label1.attributedText = str;
}
}