一、图文混排的概念
● 在view上显示的数据既包括文本数据,也包括图片数据。
● 图片的大小可以不同,图片和文字的排列顺序不固定。
● 文本数据的字体、颜色、背景等其他属性可以不同。
● 文本数据中还可以包含超链接。可以点击超链接进入其他页面。
二、研究FTCoreText框架的实现原理
1.此框架的功能 和 缺陷
1.1 功能:此框架能够实现不同风格的文本数据、图片数据的混合显示,还可以设置超链接。
1.2 缺陷:此框架里面的文本的处理只针对与XML或者HTML格式的数据。(比如这种标签类型的数据:春暖花开)对于JSON格式的数据,可能需要自己修改内部一些方法的实现。
2.此框架的基本构成
2.1 此框架有两个类组成,分别是FTCoreTextStyle类、FTCoreTextView类。
2.2 FTCoreTextStyle类主要是用于设置格式的,定义了一些属性和方法。通过这个类就可以定义一种格式。
2.3 FTCoreTextView这个类是此框架的核心类。这个类里面定义了一个私有的类FTCoreTextNode。需要处理的数据中,每一个标签(春暖花开)对应一个节点。节点类里面就封装了一些节点的属性和方法,供FTCoreTextView这个类使用。
2.4 FTCoreTextView这个类主要封装了对数据的处理、绘制图片和文本,其中包含了很多处理细节的方法。
2.5 IOS8新特性:使用FTCoreText前无须导入CoreText.framework框架。
3.FTCoreText框架内部具体的实现原理
3.1 FTCoreTextStyle类的主要实现
3.1.1 FTCoreTextStyle类封装的一些重要的属性:
@property (nonatomic) NSString *name; //格式的名称
@property (nonatomic) NSString *appendedCharacter; //追加的字符串
@property (nonatomic) UIFont *font; //字体
@property (nonatomic) UIColor *color; //颜色
@property (nonatomic, getter=isUnderLined) BOOL underlined; //下划线
@property (nonatomic) FTCoreTextAlignement textAlignment; //文字显示位置
@property (nonatomic) UIEdgeInsets paragraphInset; //段落之间的间隔
@property (nonatomic) CGFloat leading;
@property (nonatomic) CGFloat maxLineHeight; //最大行高
@property (nonatomic) CGFloat minLineHeight; //最小行高
// bullet 格式
@property (nonatomic) NSString *bulletCharacter; //设置bullet字符
@property (nonatomic) UIFont *bulletFont; // 设置bullet字体
@property (nonatomic) UIColor *bulletColor; //设置bullet颜色
//当格式被解析时,回调的block
@property (nonatomic, copy) FTCoreTextCallbackBlock block;
@property (nonatomic, assign) BOOL applyParagraphStyling; //段落格式</span>
3.1.2 FTCoreTextStyle类封装的一些重要的方法:
//初始化方法
- (id)init
{
self = [super init];
if (self) {
self.name = @"_default";
self.bulletCharacter = @"•";
self.appendedCharacter = @"";
self.font = [UIFont systemFontOfSize:12];
self.color = [UIColor blackColor];
self.underlined = NO;
self.textAlignment = FTCoreTextAlignementLeft;
self.maxLineHeight = 0;
self.minLineHeight = 0;
self.paragraphInset = UIEdgeInsetsZero;
self.applyParagraphStyling = YES;
self.leading = 0;
self.block=nil;
}
return self;
}
//类方法,通过name来定义一种格式
+ (id)styleWithName:(NSString *)name
{
FTCoreTextStyle *style = [[FTCoreTextStyle alloc] init];
[style setName:name];
return style;
}
3.2 FTCoreTextNode的主要实现
3.2.1 FTCoreTextNode封装的一些主要的属性:
@property (nonatomic, assign) FTCoreTextNode *supernode; //父节点
@property (nonatomic) NSArray *subnodes; //所有子类节点
@property (nonatomic, copy) FTCoreTextStyle *style; //定义一种格式
@property (nonatomic) NSRange styleRange; //定义格式应用的范围
@property (nonatomic) BOOL isClosed; //是否是关闭
@property (nonatomic) NSInteger startLocation; //开始的位置
@property (nonatomic) BOOL isLink; //是否是链接
@property (nonatomic) BOOL isImage; //是否是图片
@property (nonatomic) BOOL isBullet; //是否是Bullet
@property (nonatomic) NSString *imageName; //图片的名称
3.2.2 FTCoreTextNode封装的一些主要的方法:
//在所有子节点的最后插入一个子节点
- (void)addSubnode:(FTCoreTextNode *)node
{
[self insertSubnode:node atIndex:[_subnodes count]];
}
//在指定的位置插入子节点
- (void)insertSubnode:(FTCoreTextNode *)subnode atIndex:(NSUInteger)index
{
//设置父节点
subnode.supernode = self;
NSMutableArray *subnodes = (NSMutableArray *)self.subnodes;
//判断index是否超出数组_subnodes的长度
if (index <= [_subnodes count]) {
[subnodes insertObject:subnode atIndex:index];
}
else {
[subnodes addObject:subnode];
}
}
//获取节点的索引
- (NSUInteger)nodeIndex
{
return [_supernode.subnodes indexOfObject:self];
}
//获取指定位置的子节点
- (FTCoreTextNode *)subnodeAtIndex:(NSUInteger)index
{
if (index < [_subnodes count]) {
return [_subnodes objectAtIndex:index];
}
return nil;
}
3.3 FTCoreTextView类的主要实现
3.3.1 FTCoreTextView封装的一些主要的属性:
@property (nonatomic) NSString *text; //文本内容
@property (nonatomic) NSString *processedString; //处理之后的文本
@property (nonatomic, readonly) NSAttributedString *attributedString; //属性文本
@property (nonatomic, assign) CGPathRef path; //路径
@property (nonatomic) NSMutableDictionary *URLs; //存储所有的URL
@property (nonatomic) NSMutableArray *images; //存储所有图片
@property (nonatomic, assign) id <FTCoreTextViewDelegate> delegate; //设置代理对象
@property (nonatomic) UIColor *shadowColor; //阴影颜色
@property (nonatomic) CGSize shadowOffset; //阴影大小
@property (nonatomic) BOOL verbose;
@property (nonatomic) BOOL highlightTouch; //触摸时高亮状态
@property (nonatomic) CTFramesetterRef framesetter;
@property (nonatomic) FTCoreTextNode *rootNode; //节点
@property (nonatomic, readwrite) NSAttributedString *attributedString; //属性文本
@property (nonatomic) NSDictionary *touchedData; //点击的文本数据的属性的key和value的映射
@property (nonatomic) NSArray *selectionsViews; //选中的视图
//以下常量是一些默认的标记(标签)名称
extern NSString * const FTCoreTextTagDefault; //默认标记
extern NSString * const FTCoreTextTagImage; //图片标记
extern NSString * const FTCoreTextTagBullet; //Bullet标记
extern NSString * const FTCoreTextTagPage; //< _page / >分页标记
extern NSString * const FTCoreTextTagLink; //超链接标记
extern NS