2012-03-25 77 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