Space between the last 2 lines of a paragraph is larger?

只愿长相守 提交于 2019-12-01 11:19:04
Saykho

If you use NSMutableAttributeString for text layout, you can set a CTRunDelegate attribute to set the \n metrics to 0. For me this worked:

        CTRunDelegateCallbacks callbacks;
        callbacks.version = kCTRunDelegateVersion1;
        callbacks.getAscent = lineBreakCallback;
        callbacks.getDescent = lineBreakCallback;
        callbacks.getWidth = lineBreakCallback;

        CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"System", 1.0f, NULL);

        CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, NULL); //3
        NSDictionary *attrDictionaryDelegate = [NSDictionary dictionaryWithObjectsAndKeys:
                                                //set the delegate
                                                (__bridge id)delegate, (NSString*)kCTRunDelegateAttributeName,
                                                (__bridge id)fontRef, kCTFontAttributeName,
                                                nil];
        stringLength++;
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:attrDictionaryDelegate]];

        CFRelease(delegate);
        CFRelease(fontRef);

and

static CGFloat lineBreakCallback( void* ref )
{
    return 0;
}

EDIT:

  • following comments I fixed memory management part (I hope that correctly)
  • I added a font attribute with font of size 1. This is because when the font size of a run (default font size is about 16) is bigger than the rest of line, it changes the line metrics even if the run metrics are smaller (which is quite annoying if you really want to set a bigger font size to a part of a line without for example changing the line's descent - I haven't found a solution for this problem yet).

Damit, it's a bug. DTCoreText works around this by repositioning the baseline origins of those affected lines.

see http://www.cocoanetics.com/2012/02/radar-coretext-line-spacing-bug/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!