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

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

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

使用开源代码 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 [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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 04:58:03

富文本的封装-NSAttributedString 的简易封装的相关文章

富文本常用封装(NSAttributedString浅析)

最近经常遇到关于富文本的一些需求,特此封装了几个最常用的API分享给大家,但授之以鱼不如授之以渔,接下来会顺便谈谈NSAttributedString,确保你读了本篇文章能够自己封装关于富文本的API,本文封装API的示例Demo再此,拿去用吧!骚年们! 一.常用需求封装 需求:在我们日常开发中,某些句子中会有改变某些字颜色的需求,当然颜色一般而言就是为了着重强调,常为同一种颜色,所以下面代码是单纯改变一句话中的某些字的颜色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

富文本常用封装(NSAttributedString)

最近经常遇到关于富文本的一些需求,特此封装了几个最常用的API分享给大家,但授之以鱼不如授之以渔,接下来会顺便谈谈NSAttributedString,确保你读了本篇文章能够自己封装关于富文本的API,本文封装API的示例Demo再此,拿去用吧!骚年们! 一.常用需求封装 需求:在我们日常开发中,某些句子中会有改变某些字颜色的需求,当然颜色一般而言就是为了着重强调,常为同一种颜色,所以下面代码是单纯改变一句话中的某些字的颜色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

「wxParser」小程序插件:想在小程序中快速部署富文本?这个插件让你一步搞定

上期,我们在<「微信同声传译」小程序插件:快速实现语音转文字.文本翻译.语音合成等能力>一文中介绍了「微信同声传译」小程序插件的意义.作用以及应用.而在此之前,我们还介绍过「腾讯地图」.「腾讯视频」.「医院 LBS 位置服务」插件,有兴趣了解的读者可以点击「微信极客WeGeek」公众号底部菜单「极客干货 - 小程序插件」了解. 今天我们为大家推荐的是一款富文本渲染插件「wxParser」,目前 wxParser 支持对一般的富文本内容包括标题.字体大小.对齐和列表等进行解析.同时也支持表格.代

iOS富文本-NSAttributedString简单封装

直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>/** *  基类富文本 */@interface WJAttributeStyle : NSObject @property (nonatomic,strong)NSString *attributeName;@property (nonatomic,strong)

实现一个简易的富文本编辑器(二):给富文本添加自定义事件

为富文本添加一个提交按钮,点击按钮可以获取富文本内容.但是在提交前或者提交后我想做一些操作,比如内容校验内容清空等等. 我们直接在该按钮上绑定点击事件同样可以达到目的,但是为了组件化,所以本例打算为提交按钮自定义beforeSubmit.afterSubmit两个事件. 1.创建发布订阅者对象 前文说到,事件系统是发布-订阅模式的一个实现,模式给事件发布函数与事件处理函数进行解耦,使得两者无直接调用关系. 简易发布订阅者对象实现如下: var Event = { // _cachePool :

快速简易封装歌词文件

月半夜小夜曲.lrc [ti:月半夜小夜曲] [ar:李克勤] [by:TTPod] [00:01.48]月半小夜曲 [00:05.66]作词:向雪怀 [00:10.66]作曲:河合奈保子 [00:15.63]演唱:李克勤 [00:20.63] [00:24.56]仍然倚在失眠夜望天边星宿 [00:30.46]仍然听见小提琴如泣似诉再挑逗 [00:36.30]为何只剩一弯月留在我的天空 [00:42.92]这晚以后音讯隔绝 [00:48.29]人如天上的明月是不可拥有 [00:54.26]情如曲

对JdbcTemplate进行简易封装以使其更加易用

在上一篇博文<基于SpringJDBC的类mybatis形式SQL语句管理的思考与实现>中,我们实现了采用XML文件对SQL进行管理.在本篇博文中,我们将对SpringJDBC提供的JdbcTemplate进行简易封装,使其更加的易用,更加贴近上篇博文中对于SQL管理的设计. 我们希望在使用将要封装的这个工具进行数据库操作时有以下几个优势: 不处理数据获取异常 不关心日志记录 即可以使用我们XML文件中的SQL语句,也可以使用在业务方法中定义的SQL语句.(因为我们设计的XML文件并不能够非常

AVAudioPlayer简易封装

[说明] AVAudioPlayer简易封装,仅仅支持播放,暂停,停止,暂停时候带有渐隐效果,自己用,没有参考价值. [源码] https://github.com/YouXianMing/AVAudioPlayer- 一个定时器的封装类源码(该定时器可以指定运行的次数) // // SpecialTimer.h // Music // // Created by XianMingYou on 15/4/13. // Copyright (c) 2015年 XianMingYou. All ri

面localStorage用作数据缓存的简易封装

面localStorage用作数据缓存的简易封装 最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限制,因此封装了下面这个对象. 我的封装非常直观简单,比网上的一些晦涩的代码明显小巧精简实用.目前只自动回收过期或最后一次访问时间到现在的间隔最大的项,以后有时间,再把访问次数纳入到自动回收的算法中. window.MyCache = window.M