2012-03-25 52 views
2

我用CTFramesetter绘制文字,我把kCTParagraphStyleSpecifierParagraphSpacing,kCTParagraphStyleSpecifierLineSpacing,kCTParagraphStyleSpecifierParagraphSpacingBefore都设为0.0。段落的最后2行之间的空间较大?

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

目前正处在这个图像中共有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

回答

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)即使运行指标较小(如果您真的想将较大的字体大小设置为某一行的某一部分而不改变行的下降,这非常令人讨厌 - 我还没有找到' t找到了解决这个问题的方法)。
+0

感谢您提供解决方案。对于内存管理部分,你应该使用'(__bridge id)'并且稍后使用'CFRelease'释放委托对象,因为'CTRunDelegate'不是免费桥接的,即没有对象的基础对象。即使它是桥接的,你使用'__bridge_transfer'的方式是不正确的,你应该将__bridge_transfer对象保存为一个实例变量,否则它可能在被调用之前被释放。 – neevek 2012-07-31 13:37:50