iOS 基于CoreText的排版引擎

CoreText简介

CoreText是用于处理文字和字体的底层技术,它直接和Core Graphic(又称为Quartz)打交道。Quartz是一个2d图形渲染引擎,能够处理OS X和iOS的图形显示问题。

Quartz能够直接处理字体和字形,将文字渲染到界面上,也是基础库中为一个可以处理字形的模块。因此,CoreText为了排版,会将需要显示的文本内容,位置和字体字形直接传给Quartz。与其他UI组件相比,它直接与Quartz交互,所以它具有高效的排版功能。

  • 底层架构图
UILabel UITextField UITextView UIWebView
TextKit WebKit
CoreText
Core Graphic

UIWebView本身可以作为复杂排版的备选方案

基于CoreText和基于UIWebView相比,具有以下不同之处:
CoreText UIWebView
占用内存更少,渲染更快 占用内存多,渲染速度慢
渲染之前就可以精确获得显示内容的高度(只要有CFrame) 只有渲染出内容之后才能获得内容的高度(而且需要配合JS)
可以后台线程渲染 只能在主线程渲染
可以做出更好的原生交互效果,交互更细腻 交互需要依靠JS来实现,交互有卡顿
渲染内容不能方便的支持复制 --
需要自己出伏复杂的显示逻辑和响应事件 --
开始代码之前需要清楚几个点

渲染层级如下:

截屏2021-12-28 上午11.09.15.png
CTFrame
CTLine
CTRun - CTRun - CTRun

我们无法直接控制CTRun的创建过程,但是可以设置\color{red}{CTRunDelegate}控制文本的高度、宽度、对齐方式

对于图片排版,\color{red}{CoreText}本身是不支持的,但我们可以用特殊空白字符代替\color{red}{0xFFFC}
同时设置该字体的\color{red}{CTRunDelegate}为要显示的图片的宽度和高度,这样最后生成的CTFrame实例就会在绘制时,将图片位置预留出来

示例代码

CoreText

整个示例可以分为4个类:

  • \color{#e9122e}{CADisplayView} 用于绘制和显示
  • \color{#e9122e}{CoreTextData} 用于保存会只需要的数据
  • \color{#e9122e}{CTFrameParse} 用于解析本地json数据,或者网络下载的json数据
  • \color{#e9122e}{CTFrameParseConfig} 用于配置绘制的参数,例如文字大小,颜色,行间距

一、CADisplayView

  • 获取画布、设置矩阵、坐标系翻转、镜像翻转是标配
  • 绘制之前,CoreTextData 要赋值
  • 图片需要单独计算坐标并绘制
#import <UIKit/UIKit.h>
#import "CoreTextData.h"                                                                      

@interface CADisplayView : UIView

@property (nonatomic, strong) CoreTextData *data;

@end

@implementation CADisplayView

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    // 获取画布上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();
    
    // 坐标系上下翻转
    //对于底层的绘制引擎来说,左下角是(0,0)点,对于UIKit左上角是(0,0)点
    //为了原点一致需要翻转坐标系
    CGContextSetTextMatrix(ref, CGAffineTransformIdentity);
    //翻转后需要将坐标系平移 
    CGContextTranslateCTM(ref, 0, self.bounds.size.height);
    // 镜像翻转
    CGContextScaleCTM(ref, 1, -1);
    
    // 文本帧绘制
    if (self.data)
    {
        CTFrameDraw(self.data.ctframe, ref);
    }
    // 富文本中图片绘制
    for (ImageElementModel *model in self.data.imageArr)
    {
        UIImage *image=  [UIImage imageNamed:model.name];
        CGContextDrawImage(ref, model.frame, image.CGImage);
    }
}

二、CoreTextData

  • 很多cell在渲染之前需要计算出高度,所以这里提供了内容高度计算
  • 图片需要单独绘制,图片数组保存在data
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
#import "ImageElementModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface CoreTextData : NSObject

@property (nonatomic, assign) CTFrameRef ctframe; // 文本帧
@property (nonatomic, assign) NSInteger height; // 内容高度
@property (nonatomic, strong) NSArray *imageArr; //图片数组

@end

#import "CoreTextData.h"

@implementation CoreTextData


 - (void)setImageArr:(NSArray *)imageArr
{
    _imageArr = imageArr;
    [self calculateImageRectWithFrame];
}

// 在保存图片时计算图片具体的位置
-(void)calculateImageRectWithFrame
{
    // 从CTFrame中获取所有的Line
    NSArray * arrLines = (NSArray *)CTFrameGetLines(self.ctframe);
    NSInteger count = [arrLines count];
    CGPoint points[count];
    获取每行的原点
    CTFrameGetLineOrigins(self.ctframe, CFRangeMake(0, 0), points);
    
    for (int i = 0; i < count; i ++)
    {
        CTLineRef line = (__bridge CTLineRef)arrLines[I];
        //获取所有的CTRun
        NSArray * arrGlyphRun = (NSArray *)CTLineGetGlyphRuns(line);
        for (int j = 0; j < arrGlyphRun.count; j ++)
        {
            CTRunRef run = (__bridge CTRunRef)arrGlyphRun[j];
            NSDictionary * attributes = (NSDictionary *)CTRunGetAttributes(run);
            
            // 获取delegate
            CTRunDelegateRef delegate = (__bridge CTRunDelegateRef)[attributes valueForKey:(id)kCTRunDelegateAttributeName];
            
            if (delegate == nil)
            {
                continue;
            }
 
            // 获取之前创建delegate传入的dict            
            NSDictionary * dic = CTRunDelegateGetRefCon(delegate);
            if (![dic isKindOfClass:[NSDictionary class]])
            {
                continue;
            }
            CGPoint point = points[I];
            CGFloat ascent;
            CGFloat descent;
            //相对位置
            CGRect boundsRun;
            // 一个图片被特殊字符代替后只是一个CTRun,下面也只会调用一次
            boundsRun.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, NULL);
            boundsRun.size.height = ascent + descent;
            CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL);
            boundsRun.origin.x = point.x + xOffset;
            boundsRun.origin.y = point.y - descent;
            
            CGPathRef path = CTFrameGetPath(self.ctframe);
            //获得View的位置
            CGRect colRect = CGPathGetBoundingBox(path);
            //相对偏移获取image的实际位置
            CGRect imageBounds = CGRectOffset(boundsRun, colRect.origin.x, colRect.origin.y);
            
            NSNumber *index = [dic objectForKey:@"index"];
            
            ImageElementModel *model = [self.imageArr objectAtIndex:[index intValue]];
            
            model.frame = imageBounds;
        }
    }
}

- (void)dealloc
{
    CFRelease(self.ctframe);
}
@end

三、CTFrameParse

  • 所有解析结构都是根据业务来的,这里只是参考
  • Frame作为返回值,不能立即释放,绘制完成会才可以释放
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "CoreTextData.h"
#import "CTFrameParseConfig.h"

NS_ASSUME_NONNULL_BEGIN

@interface CTFrameParse : NSObject

+ (CoreTextData *)parseJsonData:(NSString *)path config:(CTFrameParseConfig *)config;


@end

#import "CTFrameParse.h"
#import "TextElementModel.h"
#import <CoreText/CoreText.h>
#import "UIColor+Hex.h"

@implementation CTFrameParse

+ (CoreTextData *)parseJsonData:(NSString *)path config:(nonnull CTFrameParseConfig *)config
{
    CoreTextData *data = [[CoreTextData alloc] init];
    
    NSData *jsonData = [NSData dataWithContentsOfFile:path];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc] init];
    
    NSMutableArray *imageArr = [NSMutableArray array];
    
    for (NSDictionary *dict in arr)
    {
        NSString *type = [dict objectForKey:@"type"];
        if ([type isEqualToString:@"txt"])
        {
            [self parseText:dict result:resultString config:config];
        }
        else if ([type isEqualToString:@"img"])
        {
            [self parseImageData:dict result:resultString imageArr:imageArr config:config];
        }
    }
    
    CTFramesetterRef setter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)resultString);
    
    
    // 计算绘制区域高度
    CGSize restrictSize = CGSizeMake(config.width, CGFLOAT_MAX);
    
    CGSize textSize = CTFramesetterSuggestFrameSizeWithConstraints(setter, CFRangeMake(0, 0), nil, restrictSize, nil);
    CGMutablePathRef pathRef = CGPathCreateMutable();
    UIScreen *screen = [UIScreen mainScreen];
    
    if (textSize.height > screen.bounds.size.height)
    {
        CGPathAddRect(pathRef, NULL, CGRectMake(0, 0, config.width, screen.bounds.size.height));
    }
    else
    {
        CGPathAddRect(pathRef, NULL, CGRectMake(0, 0, config.width, textSize.height));
    }
    
    CTFrameRef frame = CTFramesetterCreateFrame(setter, CFRangeMake(0, resultString.length), pathRef, nil);
    
    data.ctframe = frame;
    data.height = textSize.height;
    data.imageArr = imageArr;
    // 书中的代码有问题,这里不能释放
//    CFRelease(frame);
    CFRelease(pathRef);
    CFRelease(setter);
    return data;
}


+ (void)parseText:(NSDictionary *)dict
           result:(NSMutableAttributedString *)result
           config:(nonnull CTFrameParseConfig *)config
{
    NSString *content = [dict objectForKey:@"content"];
    UIColor *color = [UIColor colorWithHexString:[dict objectForKey:@"color"]];
    UIFont *font = [UIFont systemFontOfSize:[[dict objectForKey:@"size"] floatValue]];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = config.lineSpace;
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.firstLineHeadIndent = 2;
    
    NSAttributedString *as = [[NSAttributedString alloc] initWithString:content attributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color, NSParagraphStyleAttributeName:style}];
    [result appendAttributedString:as];

}

+ (void)parseImageData:(NSDictionary *)dict
                result:(NSMutableAttributedString *)result
              imageArr:(NSMutableArray *)imageArr
                config:(nonnull CTFrameParseConfig *)config
{
    NSString *url = [dict objectForKey:@"url"];
    NSString *name = [dict objectForKey:@"name"];
    NSString *width = [dict objectForKey:@"width"];
    NSString *height = [dict objectForKey:@"height"];
    
    ImageElementModel *model = [[ImageElementModel alloc] init];
    model.url = url;
    model.name = name;
    model.width = [width floatValue];
    model.height = [height floatValue];
    
    [imageArr addObject:model];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:[NSNumber numberWithFloat:model.width] forKey:@"width"];
    [dic setValue:[NSNumber numberWithFloat:model.height] forKey:@"height"];
    [dic setValue:[NSNumber numberWithLong:imageArr.count-1] forKey:@"index"];
    model.dict = dic;
    
    NSAttributedString *as = [self replaceImageDataWithWhiteSpace:model config:config];
    [result appendAttributedString:as];
}

static CGFloat ascentCallback(void *bef)
{
    return [(NSNumber *)[(__bridge NSDictionary *)bef objectForKey:@"height"] floatValue];
}

static CGFloat descentCallback(void *bef)
{
    return 0;
}

static CGFloat widthCallback(void *bef)
{
    return [(NSNumber *)[(__bridge NSDictionary *)bef objectForKey:@"width"] floatValue];
}


+ (NSAttributedString *)replaceImageDataWithWhiteSpace:(ImageElementModel *)model
                                                config:(nonnull CTFrameParseConfig *)config
{

    CTRunDelegateCallbacks callbacks;
    memset(&callbacks, 0, sizeof(CTRunDelegateCallbacks));
    callbacks.version = kCTRunDelegateVersion1;
    callbacks.getAscent = ascentCallback;
    callbacks.getWidth = widthCallback;
    callbacks.getDescent = descentCallback;
    
    CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void * _Nullable)(model.dict));
    
    // 使用0xFFFC作为空白站位符
    unichar ojectReplacement = 0xFFFC;
       
   NSString * placeHolderStr = [NSString stringWithCharacters:&ojectReplacement length:1];
   
    NSMutableAttributedString *atStr = [[NSMutableAttributedString alloc] initWithString:placeHolderStr];
    
    CFAttributedStringSetAttribute((CFMutableAttributedStringRef)atStr, CFRangeMake(0, 1), kCTRunDelegateAttributeName, delegate);
    
    CFRelease(delegate);
    return atStr;
}

@end

demo工程

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容