富文本的封装-NSAttributedString 的简易封装

有时我们经常写富文老是写出这样子的出来,极易出错而且可读性非常差,如下:

- (void)typeOne{

    NSString *string                            =@"你好,CSDN!";

    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:string];

    // 设置富文本样式

    [attributedString addAttribute:NSForegroundColorAttributeName

                             value:[UIColorredColor]

                             range:NSMakeRange(0,1)];

    

    [attributedStringaddAttribute:NSFontAttributeName

                            value:[UIFontsystemFontOfSize:24.f]

                            range:NSMakeRange(0,2)];

    

    UILabel *label       = [[UILabelalloc] initWithFrame:CGRectMake(0,100,320, 30)];

    label.attributedText = attributedString;

    [self.viewaddSubview:label];

}


- (void)typeTwo {

    NSString *string                            = @"你好,CSDN!";

    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:string];

    

    // 设置富文本样式

    [attributedString addAttribute:NSForegroundColorAttributeName

                             value:[UIColorredColor]

                             range:NSMakeRange(0,1)];

    

    [attributedStringaddAttribute:NSFontAttributeName

                            value:[UIFontsystemFontOfSize:24.f]

                            range:NSMakeRange(0,2)];

    

    UILabel *label       = [[UILabelalloc] initWithFrame:CGRectMake(0,100,320, 30)];

    label.attributedText = attributedString;

    [self.viewaddSubview:label];

}


- (void)typeThree {

    NSString *string =@"你好,CSDN!你好,CSDN!\n你好,CSDN!";

    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:string];

    

    // 设置富文本 - 段落样式

    NSMutableParagraphStyle *style = [[NSMutableParagraphStylealloc] init];

    style.lineSpacing              =10.f;

    style.paragraphSpacing         =20.f;

    

    [attributedString addAttribute:NSParagraphStyleAttributeName

                             value:style

                             range:NSMakeRange(0, string.length)];

    

    UILabel *label       = [[UILabelalloc] initWithFrame:CGRectMake(0,100,320, 400)];

    

    // 设置段落样式的时候,numberOfLines必须为0

    label.numberOfLines  =0;

    label.attributedText = attributedString;

    [self.viewaddSubview:label];

}


我们可以封装,一个父类AttributedStyle,颜色和字体类都继承父类AttributedStyle,再写一个NSString的分类,如下:


#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface AttributedStyle :NSObject

@property (nonatomic,strong)NSString  *attributeName;

@property (nonatomic,strong)id         value;

@property (nonatomic)       NSRange    range;


/**

 *  便利构造器

 *

 *  @param attributeName属性名字

 *  @param value        设置的值

 *  @param range        取值范围

 *

 *  @return 实例对象

 */

+ (AttributedStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range;


@end


#import "AttributedStyle.h"

@implementation AttributedStyle


+ (AttributedStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range {

    

    AttributedStyle *style = [[selfclass] new];

    

    style.attributeName = attributeName;

    style.value         = value;

    style.range         = range;

    

    return style;

}

@end


#import "AttributedStyle.h"

@interface ForeGroundColorStyle :AttributedStyle

+ (id)withColor:(UIColor *)color range:(NSRange)range;

@end


/**

 *  内联函数

 *

 *  @param color 颜色

 *  @param range 范围

 *

 *  @return 实例对象

 */

staticinline AttributedStyle* colorStyle(UIColor *color,NSRange range) {

    return [ForeGroundColorStylewithColor:color range:range];

}


#import "ForeGroundColorStyle.h"

@implementation ForeGroundColorStyle

+ (id)withColor:(UIColor *)color range:(NSRange)range {

    id style = [superattributeName:NSForegroundColorAttributeName

                             value:color

                             range:range];

    

    return style;

}

@end


#import "AttributedStyle.h"

@interface FontStyle :AttributedStyle

+ (id)withFont:(UIFont *)font range:(NSRange)range;

@end


/**

 *  内联函数

 *

 *  @param font  字体

 *  @param range 范围

 *

 *  @return 实例对象

 */

staticinline AttributedStyle* fontStyle(UIFont *font,NSRange range) {

    return [FontStylewithFont:font range:range];

}


#import "FontStyle.h"

@implementation FontStyle

+ (id)withFont:(UIFont *)font range:(NSRange)range {

    id fontStyle = [superattributeName:NSFontAttributeName

                                 value:font

                                 range:range];

    

    return fontStyle;

}

@end


#import "FontStyle.h"

@implementation FontStyle

+ (id)withFont:(UIFont *)font range:(NSRange)range {

    id fontStyle = [superattributeName:NSFontAttributeName

                                 value:font

                                 range:range];

    

    return fontStyle;

}

@end


#import <Foundation/Foundation.h>

#import "AttributedStyle.h"

@interface NSString (AttributeStyle)


/**

 *  根据styles数组创建出富文本

 *

 *  @param styles AttributedStyle对象构成的数组

 *

 *  @return 富文本

 */

- (NSAttributedString *)createAttributedStringWithStyles:(NSArray *)styles;

@end


#import "NSString+AttributeStyle.h"

@implementation NSString (AttributeStyle)

- (NSAttributedString *)createAttributedStringWithStyles:(NSArray *)styles {

    if (self.length <=0) {

        return nil;

    }

    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:self];

    for (int count =0; count < styles.count; count++) {

        AttributedStyle *style = styles[count];

        [attributedStringaddAttribute:style.attributeName

                                value:style.value

                                range:style.range];

    }

    return attributedString;

}


@end


如下使用,简单多了,清晰明了

#import "NSString+AttributeStyle.h"

#import "ForeGroundColorStyle.h"

#import "FontStyle.h"


- (void)viewDidLoad {

    [superviewDidLoad];

    NSString *string = @"你好,CSDN!";

    UILabel *label       = [[UILabelalloc] initWithFrame:CGRectMake(0,100,320, 30)];

    label.attributedText = \

    [string createAttributedStringWithStyles:\

     @[colorStyle([UIColorredColor],             NSMakeRange(0,2)),

       fontStyle ([UIFontsystemFontOfSize:20.f],NSMakeRange(1,2))]];

    

    [self.viewaddSubview:label];

}

@end


使用开源代码 GONMarkupParser处理富文本

GONMarkupParse 下载地址:http://download.csdn.net/detail/baitxaps/8912439


#import <Foundation/Foundation.h>

#import "GONMarkupParser_All.h"


@interface NSString (GONMarkupDemo)


- (NSString *)addColorMark:(NSString *)mark;


- (NSAttributedString *)createAttributedString;


@end


#import "NSString+GONMarkupDemo.h"

@implementation NSString (GONMarkupDemo)


- (NSString *)addColorMark:(NSString *)mark {

    

    if (self.length <=0) {

        return nil;

    }

    

    return [NSStringstringWithFormat:@"<color value=\"%@\">%@</>", mark,self];

}


- (NSAttributedString *)createAttributedString {

    return [[GONMarkupParserManagersharedParser] attributedStringFromString:self

                                                                      error:nil];

}


@end


#import "NSString+GONMarkupDemo.h"


- (void)viewDidLoad {

    [superviewDidLoad];

    

    NSString *strOne   = @"My";

    NSString *strTwo   = @"name";

    NSString *string   = [NSStringstringWithFormat:@"%@ %@ is XX",

                          [strOneaddColorMark:@"red"],

                          [strTwoaddColorMark:@"green"]];

    

    

    UILabel *label       = [[UILabelalloc] initWithFrame:CGRectMake(0,100,320, 200)];

    label.numberOfLines  =0;

    label.attributedText = [stringcreateAttributedString];

    [self.viewaddSubview:label];

}


@end


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值