4

我写了一个代码,使用 CoreText 布局一些文本,如下图所示。红色矩形为文本区域,黑色矩形为图像或其他对象为空。我使用kCTFrameClippingPathsAttributeName告诉 CoreText,黑色矩形不会显示文本。

核心文本文本布局

详细图片如下:

在此处输入图像描述

为什么有些文字会在这个位置撕裂?红色矩形和黑色矩形只有一个很小的区域,大小只有 1px 宽度,为什么会出现文字?

右边红色矩形框是(533, 40, 440, 668),右边小矩形框是(534, 98, 440, 399)

像这样的代码:

CGPathRef textArea; 

// An array of clipping paths
NSMutableArray * clippingPaths = [NSMutableArray array];
for (TNPageMedium *medium in self.media) {
    NSDictionary *clippingPathDict = @{(NSString *)kCTFramePathClippingPathAttributeName:(__bridge id)path};
    [clippingPaths addObject:clippingPathDict];
}

NSDictionary  *optionsDict = @{(NSString*)kCTFrameClippingPathsAttributeName : clippingPaths};

// create frame using texAreaPath, an optionDictionary contains clipping paths
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(pos, 0), textAreaPath, (__bridge CFDictionaryRef)(optionsDict));

// Draw Frame
CTFrameDraw(frame, context);
4

1 回答 1

2

看起来您attributedString缺少段落样式信息。

我猜你是从创建NSAttributedStringNSString,并且没有kCTParagraphStyleAttributeName在属性字典中提供键值。

于 2012-11-26T13:19:58.570 回答