您的位置:首页 > 其它

利用NSAttributeString进行图文混编

2016-04-05 15:07 267 查看
NSString* showText = [NSString stringWithFormat:@"%@%@",text1,text2];
NSRange range1 = [showText rangeOfString:text1];
NSRange range2 = [showText rangeOfString:text2];

NSDictionary* attributes1 = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName:dColor
};
NSDictionary* attributes2 = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName:cColor
};
NSMutableAttributedString* attributeString = [[[NSMutableAttributedString alloc] initWithString:showText]
autorelease];
[attributeString addAttributes:attributes1 range:range1];
[attributeString addAttributes:attributes2 range:range2];

NSTextAttachment* textAttachment = [[[NSTextAttachment alloc] init] autorelease];
textAttachment.image = image;
textAttachment.bounds = CGRectMake(0, -4, 16, 16);  // 微调图片位置

NSAttributedString* imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributeString insertAttributedString:imageAttachment atIndex:range1.length]; // 插入图片

label.attributedText = attributeString;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: