【问题标题】:Space between the last 2 lines of a paragraph is larger?段落的最后两行之间的空间更大?
【发布时间】:2012-04-09 04:11:31
【问题描述】:

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

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

这张图一共15行,我贴了他们的ascentdescentleadingorigin.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 

【问题讨论】:

    标签: ios layout core-text


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      编辑:

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

      【讨论】:

      • 感谢您提供解决方案。对于内存管理部分,您应该使用(__bridge id),然后使用CFRelease 释放委托对象,因为CTRunDelegate 不是免费桥接的,即,该对象没有Foundation 对应对象。即使是toll-bridged,你使用__bridge_transfer的方式也不正确,你应该把__bridge_transfered对象保存为实例变量,否则在被调用之前可能会被释放。
      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多