如何在iPad中使用CTRunDelegate?

我是一个开发iPad应用程序,我必须使用CTRunDelegate 。 我已经定义了所有需要的callback,即CTRunDelegateGetAscentCallbackCTRunDelegateGetDescentCallbackCTRunDelegateGetWidthCallback 。 我不知道如何使用我创build的CTRunDelegateRef对象。 现在发生的事情是我的callback没有被调用。

任何在这方面的指针将不胜感激。

提前感谢。

您应该将您的运行委托添加为属性string中的一系列字符的属性。 请参阅核心文本string属性 。 在绘制时,核心文本会调用你的callback来获取字符的大小。

更新

这是绘制简单文本的示例代码(请注意,这里没有内存pipe理代码)。

 @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 

文本中“委托”和“空间”之间的空格字符的大小由运行委托来控制。