Note that there are some explanatory texts on larger screens.

plurals
  1. POCTFrame clipping first line of text
    primarykey
    data
    text
    <p>I am running into a problem while using Core Text, where the first line of the text I display in a <code>CTFrame</code> is cut off at the top, as seen in the screenshot below, with the character "B":</p> <p><img src="https://i.stack.imgur.com/nsFpS.png" alt="Example of CTFrame clipping first line"></p> <p>I think I'm doing something wrong while setting the leading in the <code>CTFrame</code>. My code is below:</p> <pre><code>- (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); NSAttributedString *myString; //Create the rectangle into which we'll draw the text CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, self.bounds); //Flip the coordinate system CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); //Setup leading (line height) CGFloat lineHeight = 25; CTParagraphStyleSetting settings[] = { { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &amp;lineHeight }, { kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &amp;lineHeight }, }; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0])); NSDictionary * attrs = [[NSDictionary alloc] initWithObjectsAndKeys: (__bridge id)CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL) , (NSString*)kCTFontAttributeName, (id)textColor.CGColor, (NSString*)kCTForegroundColorAttributeName, (__bridge id) paragraphStyle, kCTParagraphStyleAttributeName, nil]; myString = [[NSAttributedString alloc] initWithString:text attributes:attrs]; CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)myString); CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,[myString length]), path, NULL); CTFrameDraw(frame, context); CFRelease(frame); CFRelease(framesetter); CFRelease(path); } </code></pre> <p>Other SO posts (<a href="https://stackoverflow.com/questions/9017593/setting-line-spacing-in-a-ctframe">this one</a> and <a href="https://stackoverflow.com/questions/5511830/how-does-line-spacing-work-in-core-text-and-why-is-it-different-from-nslayoutm">this one</a>) are not really helpful. </p> <p>How can I prevent the <code>CTFrame</code> from clipping the first line? </p> <p><strong>--EDIT--</strong></p> <p>Reducing <code>lineheight</code> to 20:</p> <p><code>lineheight</code> from the second line onwards is respected, but the baseline of the first line of text is less than 20 below the top.</p> <p><img src="https://i.stack.imgur.com/L1w2s.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload