段落的最后两行之间的空格更大?

时间:2012-03-25 08:41:16

标签: ios layout core-text

我使用CTFramesetter绘制文本,我将kCTParagraphStyleSpecifierParagraphSpacingkCTParagraphStyleSpecifierLineSpacingkCTParagraphStyleSpecifierParagraphSpacingBefore全部设置为0.0。

正如您在图片中看到的那样,段落的最后两行之间的空间比其他段落大得多。

此图片总共有15行,我粘贴了上升下降前导 origin.y 在下面,我们可以看到第5行和第10行的上升和下降比其他行更大,我找不到任何说明符来设置以避免这种奇怪的布局。

有什么想法吗?

1  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 399.000000
2  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 374.000000
3  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 349.000000
4  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 324.000000
5  ascent=25.722656, desecent=13.699219, leading=0.720000, origin.y: 294.000000
6  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 258.000000
7  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 233.000000
8  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 208.000000
9  ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 183.000000
10 ascent=25.722656, descent=13.699219, leading=0.720000, origin.y: 153.000000
11 ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 117.000000
12 ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 92.000000
13 ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 67.000000
14 ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 42.000000
15 ascent=20.639999, descent=3.360000, leading=0.720000, origin.y: 17.000000 

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您使用NSMutableAttributeString进行文字布局,则可以设置CTRunDelegate属性以将\ n指标设置为0.对我而言,这有效:

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

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

编辑:

  • 以下评论我修复了内存管理部分(我希望正确)
  • 我添加了一个字体大小为1的字体属性。这是因为当运行的字体大小(默认字体大小约为16)大于行的其余部分时,即使运行指标,它也会更改行指标较小(如果你真的想在一条线的一部分设置一个更大的字体大小而不改变线的下降,这是非常烦人的 - 我还没有找到解决这个问题的方法)。

答案 1 :(得分:0)

该死,这是一个错误。 DTCoreText通过重新定位受影响线的基线来源来解决这个问题。

请参阅http://www.cocoanetics.com/2012/02/radar-coretext-line-spacing-bug/