Search code examples
iosipadcore-text

Why use CoreText render text layout, text can show in 1px width path?


I wrote a code use CoreText layout some texts like picture below. Red rectangle is text area, black rectangle is empty for images or other objects. I use kCTFrameClippingPathsAttributeName tell CoreText that, black rectangle will not show texts.

core text text layout

Detail picture as below:

enter image description here

Why some text rend at this position? Red rectangle and black rectangle only have a very small area, size only 1px width, why text come out?

Right red rectangle frame is (533, 40, 440, 668), right small rectangle frame is (534, 98, 440, 399).

code like that:

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);

Solution

  • Looks like your attributedString is missing paragraph style information.

    I guess you created NSAttributedString from NSString, and didn't provide kCTParagraphStyleAttributeName key-value in attributes dictionary.