2010-07-14 36 views
7

我是一個開發iPad應用程序,我必須使用CTRunDelegate。我已經定義了所有需要的回調,即CTRunDelegateGetAscentCallback,CTRunDelegateGetDescentCallback,CTRunDelegateGetWidthCallback。我不知道如何使用我創建的CTRunDelegateRef對象。現在發生的事情是我的回調沒有被調用。如何在iPad中使用CTRunDelegate?

任何在這方面的指針將不勝感激。

Thanx提前。

回答

11

您應該將您的運行委託添加爲屬性字符串中一系列字符的屬性。請參閱Core Text String Attributes。在繪製時,核心文本會調用你的回調來獲取字符的大小。

更新

這是用於繪製簡單的文本視圖中的示例代碼(注意,沒有內存管理代碼在這裏)。

@implementation View 

/* Callbacks */ 
void MyDeallocationCallback(void* refCon){ 

} 
CGFloat MyGetAscentCallback(void *refCon){ 
    return 10.0; 
} 
CGFloat MyGetDescentCallback(void *refCon){ 
    return 4.0; 
} 
CGFloat MyGetWidthCallback(void* refCon){ 
    return 125; 
} 

- (void)drawRect:(CGRect)rect { 
    // create an attributed string 
    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc]     initWithString:@"This is my delegate space"]; 

    // create the delegate 
    CTRunDelegateCallbacks callbacks; 
    callbacks.version = kCTRunDelegateVersion1; 
    callbacks.dealloc = MyDeallocationCallback; 
    callbacks.getAscent = MyGetAscentCallback; 
    callbacks.getDescent = MyGetDescentCallback; 
    callbacks.getWidth = MyGetWidthCallback; 
    CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, NULL); 

    // set the delegate as an attribute 
    CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attrString, CFRangeMake(19, 1), kCTRunDelegateAttributeName, delegate); 

    // create a frame and draw the text 
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString); 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, rect); 
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attrString.length), path, NULL); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextSetTextPosition(context, 0.0, 0.0); 
    CTFrameDraw(frame, context); 
} 

@end 

文本中「委託」和「空間」之間的空格字符大小由運行委託來控制。

+0

我已經這樣做了,但它沒有幫助。文檔本身並沒有提供關於CTRunDelegate的更多信息。如果你有任何CTRunDelegate的示例代碼,那將會很有幫助。 – tek3 2010-07-22 13:23:41

+0

我在看到你的答案之前就已經得到了答案,但是你爲了給我正確的答案而付出了努力,所以這個賞金就給你了。非常感謝您的回覆。 – tek3 2010-07-23 07:09:39

+0

嗨,我需要一些更多的幫助。你有興趣幫助我嗎? – tek3 2010-07-23 07:34:50

相關問題