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

后端 未结 2 869
谎友^
谎友^ 2021-01-15 12:48

I draw the text using CTFramesetter, I have set kCTParagraphStyleSpecifierParagraphSpacing, kCTParagraphStyleSpecifierLineSpacing, kCTParagra

相关标签:
2条回答
  • 2021-01-15 13:23

    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/

    0 讨论(0)
  • 2021-01-15 13:42

    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).
    0 讨论(0)
提交回复
热议问题