1. 程式人生 > >富文字的封裝-NSAttributedString 的簡易封裝

富文字的封裝-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