图文混排之简单运用

最近对图文混排的技术点做了点研究,希望能帮助开发中遇到困难的人!废话不多说直接看效果图

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;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容