您的位置:首页 > 其它

文本排版之----CoreText

2014-11-04 15:43 274 查看
第一次比较深入接触iOS文字排版相关内容是在12年底,实现某IM项目聊天内容的图文混排,照着nimbus的AttributedLabel和Raywenderlish上的这篇文章《Core
Text Tutorial for iOS: Making a Magazine App》改出了一个比较适用于聊天内容展现的图文混排(文字和表情)控件。

选择自己写而不是直接使用现有第三方库的原因有三:

1. 在这之前也做过一个iOS上的IM产品,当时这个模块并不是我负责,图文混排的实现非常诡异(通过二分法计算出文字所占区域大小),效率极低,所以需要重新做一个效率比较高的控件出来。

2. 看过一些开源的实现,包括OHAttribtuedLabel,DTCoreText和Nimbus,总觉得他们实现插入图片的接口有点别扭,对于上层调用者来说CoreText部分不是完全透明的:调用者需要考虑怎么用自己的图片把原来内容替换掉。(当时的印象,现在具体怎么样已经不清楚了)

3. 这是重新造轮子的机会!

直接拿了Nimbus的AttributedLabel作为基础,然后重新整理图文混排那部分的代码,调整接口,一共也就花了一个晚上的时间:拜一下Nimbus的作者们。后来也根据项目的需求做了一些小改动,比如hack iOS7下不准的问题,支持在Label上添加UIView的特性等等。最新的代码可以在github上找到:M80AttributedLabel。

不过写这篇文章最重要的原因不是为了放个代码出来,而是在闲暇时整理一下iOS/OSX文字排版相关的知识。

文字排版的基础概念

字体(Font):和我们平时说的字体不同,计算机意义上的字体表示的是同一大小,同一样式(Style)字形的集合。从这个意义上来说,当我们为文字设置粗体,斜体时其实是使用了另外一种字体(下划线不算)。而平时我们所说的字体只是具有相同设计属性的字体集合,即Font Family或typeface。

字符(Character)和字形(Glyphs):排版过程中一个重要的步骤就是从字符到字形的转换,字符表示信息本身,而字形是它的图形表现形式。字符一般就是指某种编码,如Unicode编码,而字形则是这些编码对应的图片。但是他们之间不是一一对应关系,同个字符的不同字体族,不同字体大小,不同字体样式都对应了不同的字形。而由于连写(Ligatures)的存在,多个字符也会存在对应一个字形的情况。



字形描述集(Glyphs Metris):即字形的各个参数。如下面的两张图:





边框(Bounding Box):一个假想的边框,尽可能地容纳整个字形。

基线(Baseline):一条假想的参照线,以此为基础进行字形的渲染。一般来说是一条横线。

基础原点(Origin):基线上最左侧的点。

行间距(Leading):行与行之间的间距。

字间距(Kerning):字与字之间的距离,为了排版的美观,并不是所有的字形之间的距离都是一致的,但是这个基本步影响到我们的文字排版。

上行高度(Ascent)和下行高度(Decent):一个字形最高点和最低点到基线的距离,前者为正数,而后者为负数。当同一行内有不同字体的文字时,就取最大值作为相应的值。如下图:



红框高度既为当前行的行高,绿线为baseline,绿色到红框上部分为当前行的最大Ascent,绿线到黄线为当前行的最大Desent,而黄框的高即为行间距。由此可以得出:lineHeight = Ascent + |Decent| + Leading。

更加详细的内容可以参考苹果的这篇文档: 《Cocoa
Text Architecture Guide》。当然如果要做到更完善的排版,还需要掌握段落排版(Paragragh Style)相关的知识,但是如果只是完成聊天框内的文字排版,以上的基础知识已经够用了。详细的段落样式相关知识可以参考: 《Ruler
and Paragraph Style Programming Topics》

CoreText

iOS/OSX中用于描述富文本的类是NSAttributedString,顾名思义,它比NSString多了Attribute的概念。它可以包含很多属性,粗体,斜体,下划线,颜色,背景色等等,每个属性都有其对应的字符区域。在OSX上我们只需解析完毕相应的数据,准备好NSAttributedString即可,底层的绘制完全可以交给相应的控件完成。但是在iOS上就没有这么方便,想要绘制Attributed String就需要用到CoreText了。(当然iOS6之后已经有AttributedLabel了。)

使用CoreText进行NSAttributedString的绘制,最重要的两个概念就是CTFrameSetter和CTFrame。他们的关系如下:



其中CTFramesetter是由CFAttributedString(NSAttributedString)初始化而来,可以认为它是CTFrame的一个Factory,通过传入CGPath生成相应的CTFrame并使用它进行渲染:直接以CTFrame为参数使用CTFrameDraw绘制或者从CTFrame中获取CTLine进行微调后使用CTLineDraw进行绘制。

一个CTFrame是由一行一行的CLine组成,每个CTLine又会包含若干个CTRun(既字形绘制的最小单元),通过相应的方法可以获取到不同位置的CTRun和CTLine,以实现对不同位置touch事件的响应。



图文混排的实现

CoreText实际上并没有相应API直接将一个图片转换为CTRun并进行绘制,它所能做的只是为图片预留相应的空白区域,而真正的绘制则是交由CoreGraphics完成。(像OSX就方便很多,直接将图片打包进NSTextAttachment即可,根本无须操心绘制的事情,所以基于这个想法,M80AttributedLabel的接口和实现也是使用了attachment这么个概念,图片或者UIView都是被当作文字段中的attachment。)

在CoreText中提供了CTRunDelegate这么个Core Foundation类,顾名思义它可以对CTRun进行拓展:AttributedString某个段设置kCTRunDelegateAttributeName属性之后,CoreText使用它生成CTRun是通过当前Delegate的回调来获取自己的ascent,descent和width,而不是根据字体信息。这样就给我们留下了可操作的空间:用一个空白字符作为图片的占位符,设好Delegate,占好位置,然后用CoreGraphics进行图片的绘制。以下就是整个图文混排代码描述的过程:

占位:
- (void)appendAttachment: (M80AttributedLabelAttachment *)attachment
{
attachment.fontAscent                   = _fontAscent;
attachment.fontDescent                  = _fontDescent;
unichar objectReplacementChar           = 0xFFFC;
NSString *objectReplacementString       = [NSString stringWithCharacters:&objectReplacementChar length:1];
NSMutableAttributedString *attachText   = [[NSMutableAttributedString alloc]initWithString:objectReplacementString];

CTRunDelegateCallbacks callbacks;
callbacks.version       = kCTRunDelegateVersion1;
callbacks.getAscent     = ascentCallback;
callbacks.getDescent    = descentCallback;
callbacks.getWidth      = widthCallback;
callbacks.dealloc       = deallocCallback;

CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (void *)attachment);
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)delegate,kCTRunDelegateAttributeName, nil];
[attachText setAttributes:attr range:NSMakeRange(0, 1)];
CFRelease(delegate);

[_attachments addObject:attachment];
[self appendAttributedText:attachText];
}


实现委托回调:
CGFloat ascentCallback(void *ref)
{
M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
CGFloat ascent = 0;
CGFloat height = [image boxSize].height;
switch (image.alignment)
{
case M80ImageAlignmentTop:
ascent = image.fontAscent;
break;
case M80ImageAlignmentCenter:
{
CGFloat fontAscent  = image.fontAscent;
CGFloat fontDescent = image.fontDescent;
CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
ascent = height / 2 + baseLine;
}
break;
case M80ImageAlignmentBottom:
ascent = height - image.fontDescent;
break;
default:
break;
}
return ascent;
}

CGFloat descentCallback(void *ref)
{
M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
CGFloat descent = 0;
CGFloat height = [image boxSize].height;
switch (image.alignment)
{
case M80ImageAlignmentTop:
{
descent = height - image.fontAscent;
break;
}
case M80ImageAlignmentCenter:
{
CGFloat fontAscent  = image.fontAscent;
CGFloat fontDescent = image.fontDescent;
CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
descent = height / 2 - baseLine;
}
break;
case M80ImageAlignmentBottom:
{
descent = image.fontDescent;
break;
}
default:
break;
}

return descent;

}

CGFloat widthCallback(void* ref)
{
M80AttributedLabelAttachment *image  = (__bridge M80AttributedLabelAttachment *)ref;
return [image boxSize].width;
}


真正的绘制:
- (void)drawAttachments
{
if ([_attachments count] == 0)
{
return;
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (ctx == nil)
{
return;
}

CFArrayRef lines = CTFrameGetLines(_textFrame);
CFIndex lineCount = CFArrayGetCount(lines);
CGPoint lineOrigins[lineCount];
CTFrameGetLineOrigins(_textFrame, CFRangeMake(0, 0), lineOrigins);
NSInteger numberOfLines = [self numberOfDisplayedLines];
for (CFIndex i = 0; i < numberOfLines; i++)
{
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CFArrayRef runs = CTLineGetGlyphRuns(line);
CFIndex runCount = CFArrayGetCount(runs);
CGPoint lineOrigin = lineOrigins[i];
CGFloat lineAscent;
CGFloat lineDescent;
CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, NULL);
CGFloat lineHeight = lineAscent + lineDescent;
CGFloat lineBottomY = lineOrigin.y - lineDescent;

// Iterate through each of the "runs" (i.e. a chunk of text) and find the runs that
// intersect with the range.
for (CFIndex k = 0; k < runCount; k++)
{
CTRunRef run = CFArrayGetValueAtIndex(runs, k);
NSDictionary *runAttributes = (NSDictionary *)CTRunGetAttributes(run);
CTRunDelegateRef delegate = (__bridge CTRunDelegateRef)[runAttributes valueForKey:(id)kCTRunDelegateAttributeName];
if (nil == delegate)
{
continue;
}
M80AttributedLabelAttachment* attributedImage = (M80AttributedLabelAttachment *)CTRunDelegateGetRefCon(delegate);

CGFloat ascent = 0.0f;
CGFloat descent = 0.0f;
CGFloat width = (CGFloat)CTRunGetTypographicBounds(run,
CFRangeMake(0, 0),
&ascent,
&descent,
NULL);

CGFloat imageBoxHeight = [attributedImage boxSize].height;
CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, nil);

CGFloat imageBoxOriginY = 0.0f;
switch (attributedImage.alignment)
{
case M80ImageAlignmentTop:
imageBoxOriginY = lineBottomY + (lineHeight - imageBoxHeight);
break;
case M80ImageAlignmentCenter:
imageBoxOriginY = lineBottomY + (lineHeight - imageBoxHeight) / 2.0;
break;
case M80ImageAlignmentBottom:
imageBoxOriginY = lineBottomY;
break;
}

CGRect rect = CGRectMake(lineOrigin.x + xOffset, imageBoxOriginY, width, imageBoxHeight);
UIEdgeInsets flippedMargins = attributedImage.margin;
CGFloat top = flippedMargins.top;
flippedMargins.top = flippedMargins.bottom;
flippedMargins.bottom = top;

CGRect attatchmentRect = UIEdgeInsetsInsetRect(rect, flippedMargins);

id content = attributedImage.content;
if ([content isKindOfClass:[UIImage class]])
{
CGContextDrawImage(ctx, attatchmentRect, ((UIImage *)content).CGImage);
}
else if ([content isKindOfClass:[UIView class]])
{
UIView *view = (UIView *)content;
if (view.superview == nil)
{
[self addSubview:view];
}
CGRect viewFrame = CGRectMake(attatchmentRect.origin.x,
self.bounds.size.height - attatchmentRect.origin.y - attatchmentRect.size.height,
attatchmentRect.size.width,
attatchmentRect.size.height);
[view setFrame:viewFrame];
}
else
{
NSLog(@"Attachment Content Not Supported %@",content);
}

}
}
}


详细的代码可以直接在github上查看: https://github.com/xiangwangfeng/M80AttributedLabel/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: