富文本拼接封装,包含空值处理。

    富文本的拼接经常要写一大串代码,有的时候还要拼接图片,才能达到效果,还容易搞错。如果有服务端返回的空值,还会造成程序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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值