富文本的拼接经常要写一大串代码,有的时候还要拼接图片,才能达到效果,还容易搞错。如果有服务端返回的空值,还会造成程序crash。这里就进行了封装。包含了空值处理。
NSAttributedString+AtrtributeText.h文件
#import <Foundation/Foundation.h>
@import UIKit;
@interface NSAttributedString(AtrtributeText)
//使用说明:将要进行富文本操作的字符串传入texts数组,colors传入对应的颜色数组,fonts传入对应的字体数组,colors 和fonts 中的元素的顺序要与texts中的要进行富文本操作的元素一一对应。
+ (NSAttributedString *)attributedTextArray:(NSArray *)texts
textColors:(NSArray *)colors
textfonts:(NSArray *)fonts;
//使用说明:将要进行富文本操作的字符串传入texts数组,colors传入对应的颜色数组,fonts传入对应的字体数组,colors 和fonts 中的元素的顺序要与texts中的要进行富文本操作的元素一一对应。insertIndex是图片放在texts数组中的元素个数加上1,然后看图片在这个加了1的数组中的index,即为insertIndex。imageBounds为图片的bounds。
+ (NSAttributedString *)fixAttributedStrWithTexts:(NSArray *)texts
textColors:(NSArray *)colors
textfonts:(NSArray *)fonts
image:(UIImage *)image
insertIndex:(NSInteger)index
imageBounds:(CGRect)bounds;
@end
-----------------------------------------------------------------------------------------------
NSAttributedString+AtrtributeText.m文件
#import "NSAttributedString+AtrtributeText.h"
@import CoreFoundation;
@implementation NSAttributedString(AtrtributeText)
+ (NSAttributedString *)attributedTextArray:(NSArray *)texts
textColors:(NSArray *)colors
textfonts:(NSArray *)fonts
{
if(texts.count == 0){
return nil;
}
NSMutableAttributedString *resultAttributedStr = [[NSMutableAttributedString alloc] init];
for(int i=0; i<texts.count; i++)
{
NSString *text = texts[i];
if (!text) {
text = @"";
}
NSMutableAttributedString *mAttributedStr = [[NSMutableAttributedString alloc] initWithString:text];
[mAttributedStr addAttribute:NSForegroundColorAttributeName value:colors[i] range:NSMakeRange(0, text.length)];
[mAttributedStr addAttribute:NSFontAttributeName value:fonts[i] range:NSMakeRange(0, text.length)];
[resultAttributedStr appendAttributedString:mAttributedStr];
}
return resultAttributedStr;
}
+ (NSAttributedString *)fixAttributedStrWithTexts:(NSArray *)texts
textColors:(NSArray *)colors
textfonts:(NSArray *)fonts
image:(UIImage *)image
insertIndex:(NSInteger)index
imageBounds:(CGRect)bounds
{
if(texts.count==0){
return nil;
}
NSMutableAttributedString *resultAttrStr = [[NSMutableAttributedString alloc] init];
NSInteger locationIndex = 0;
for(int i=0; i<texts.count; i++)
{
NSString *text = texts[i];
if (!text) {
text = @"";
}
NSAttributedString *tempMAttrStr = [self attributedStrWithText:texts[i] textColor:colors[i] textFont:fonts[i]];
[resultAttrStr appendAttributedString:tempMAttrStr];
// 计算图片应该插入的位置
if(index<=texts.count){
if(index == 0){
locationIndex = 0;
}
else{
if(i<index){
locationIndex+=text.length;
}
}
}
}
NSMutableAttributedString *resultAttrStr1 = [[self class] insertMAttrStr:resultAttrStr image:image imageBounds:bounds insertIndex:locationIndex];
return resultAttrStr1;
}
+ (NSMutableAttributedString *)insertMAttrStr:(NSMutableAttributedString *)mAttrStr image:(UIImage *)image imageBounds:(CGRect)bounds insertIndex:(NSInteger)index
{
if(image){
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
attachment.bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
NSAttributedString *attachmentAttrStr = [NSAttributedString attributedStringWithAttachment:attachment];
[mAttrStr insertAttributedString:attachmentAttrStr atIndex:index];
}
return [mAttrStr copy];
}
// 由text生成attributedString
+ (NSAttributedString *)attributedStrWithText:(NSString *)text textColor:(UIColor *)color textFont:(UIFont *)font
{
NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:text];
[mAttrStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, text.length)];
[mAttrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
return mAttrStr;
}
@end