码迷,mamicode.com
首页 > 其他好文 > 详细

把图片添加到文字中实现图文混排

时间:2016-05-18 10:46:12      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

NSString * introString = @"圣芭芭拉,圣莫妮卡海滩,圣地亚哥老城,科罗拉多岛";
NSArray * introArray = [introString componentsSeparatedByString:@","];

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, self.view.frame.size.width-26, [self askTextHeight:introString])];
label.font = [UIFont systemFontOfSize:15];
label.text = introString;
label.numberOfLines = 0;
label.backgroundColor = UIColorFromRGB(0xf7f7f7);
[routecell addSubview:label];

NSMutableAttributedString * mutStr = [label.attributedText mutableCopy];
for (int i = 0; i<introArray.count-1; i++) {
NSString * ssssting = introArray[i];
NSRange sssRang = [introString rangeOfString:ssssting];
UIImage * image1 = [UIImage imageNamed:@"detail_more_arrowback"];
NSTextAttachment * attachment1 = [[NSTextAttachment alloc] init];
attachment1.bounds = CGRectMake(0, 0, 37, 13);
attachment1.image = image1;
NSAttributedString * attachStr1 = [NSAttributedString attributedStringWithAttachment:attachment1];
[mutStr replaceCharactersInRange:NSMakeRange(sssRang.length+sssRang.location, 1) withAttributedString:attachStr1];
}
label.attributedText = [mutStr copy];

NSMutableAttributedString *attributedString = [label.attributedText mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:16];//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.attributedText length])];
label.attributedText = attributedString;
[label sizeToFit];

效果:

技术分享

把图片添加到文字中实现图文混排

标签:

原文地址:http://www.cnblogs.com/xilanglang/p/5504221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!