有时我们经常写富文老是写出这样子的出来,极易出错而且可读性非常差,如下:
- (void)typeOne{
NSString *string = @"你好,CSDN!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
// 设置富文本样式
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, 1)];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:24.f]
range:NSMakeRange(0, 2)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
label.attributedText = attributedString;
[self.view addSubview:label];
}
- (void)typeTwo {
NSString *string = @"你好,CSDN!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
// 设置富文本样式
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, 1)];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:24.f]
range:NSMakeRange(0, 2)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
label.attributedText = attributedString;
[self.view addSubview:label];
}
- (void)typeThree {
NSString *string = @"你好,CSDN! 你好,CSDN!\n你好,CSDN!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
// 设置富文本 - 段落样式
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 10.f;
style.paragraphSpacing = 20.f;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, string.length)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 400)];
// 设置段落样式的时候,numberOfLines必须为0
label.numberOfLines = 0;
label.attributedText = attributedString;
[self.view addSubview: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 = [[self class] 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 实例对象
*/
static inline AttributedStyle* colorStyle(UIColor *color, NSRange range) {
return [ForeGroundColorStyle withColor:color range:range];
}
#import "ForeGroundColorStyle.h"
@implementation ForeGroundColorStyle
+ (id)withColor:(UIColor *)color range:(NSRange)range {
id style = [super attributeName: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 实例对象
*/
static inline AttributedStyle* fontStyle(UIFont *font, NSRange range) {
return [FontStyle withFont:font range:range];
}
#import "FontStyle.h"
@implementation FontStyle
+ (id)withFont:(UIFont *)font range:(NSRange)range {
id fontStyle = [super attributeName:NSFontAttributeName
value:font
range:range];
return fontStyle;
}
@end
#import "FontStyle.h"
@implementation FontStyle
+ (id)withFont:(UIFont *)font range:(NSRange)range {
id fontStyle = [super attributeName: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 = [[NSMutableAttributedString alloc] initWithString:self];
for (int count = 0; count < styles.count; count++) {
AttributedStyle *style = styles[count];
[attributedString addAttribute:style.attributeName
value:style.value
range:style.range];
}
return attributedString;
}
@end
#import "NSString+AttributeStyle.h"
#import "ForeGroundColorStyle.h"
#import "FontStyle.h"
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"你好,CSDN!";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
label.attributedText = \
[string createAttributedStringWithStyles:\
@[colorStyle([UIColor redColor], NSMakeRange(0, 2)),
fontStyle ([UIFont systemFontOfSize:20.f], NSMakeRange(1, 2))]];
[self.view addSubview:label];
}
@end
#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 [NSString stringWithFormat:@"<color value=\"%@\">%@</>", mark, self];
}
- (NSAttributedString *)createAttributedString {
return [[GONMarkupParserManager sharedParser] attributedStringFromString:self
error:nil];
}
@end
#import "NSString+GONMarkupDemo.h"
- (void)viewDidLoad {
[super viewDidLoad];
NSString *strOne = @"My";
NSString *strTwo = @"name";
NSString *string = [NSString stringWithFormat:@"%@ %@ is XX",
[strOne addColorMark:@"red"],
[strTwo addColorMark:@"green"]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 200)];
label.numberOfLines = 0;
label.attributedText = [string createAttributedString];
[self.view addSubview:label];
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
富文本的封装-NSAttributedString 的简易封装
原文地址:http://blog.csdn.net/baitxaps/article/details/46946579