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

我使用CTFramesetter绘制文本,我已经将kCTParagraphStyleSpecifierParagraphSpacingkCTParagraphStyleSpecifierLineSpacingkCTParagraphStyleSpecifierParagraphSpacingBefore都设置为0.0。

正如你在图像中看到的,一个段落的最后两行之间的空间比其他的要大得多。

这幅图中总共有十五行,我把它们的上升下降领先起源粘贴在下面,我们可以看到,第五和第十行的上升和下降比别人大,我不能“find任何说明符来设置,以避免这种奇怪的布局。

有任何想法吗?

 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 

在这里输入图像说明

如果你使用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; } 

编辑:

  • 下面的评论我修正了内存pipe理部分(我希望是正确的)
  • 我添加了字体大小为1的字体属性。这是因为,当运行的字体大小(默认字体大小约为16)大于行的其余部分时,即使运行度量标准较小,也会更改行度量这是非常烦人的,如果你真的想设置一个更大的字体大小的一部分,而没有例如改变线的血统 – 我还没有find这个问题的解决scheme)。

Damit,这是一个错误。 DTCoreText通过重新定位受影响行的基线起点来解决这个问题。

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