圖文混排之簡單運用

最近對圖文混排的技術(shù)點做了點研究,希望能幫助開發(fā)中遇到困難的人!廢話不多說直接看效果圖

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;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容