我写了一个代码,使用 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);