天天看点

富文本的封装-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;

+ (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

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

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)

- (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