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;
利用NSAttributeString进行图文混编
最新推荐文章于 2020-12-01 09:38:24 发布