如何在iPad中使用CTRunDelegate?

tek*_*ek3 7 ipad core-text ios

我正在开发一个我必须使用的iPad应用程序CTRunDelegate.我已经定义了所有所需的即回调CTRunDelegateGetAscentCallback,CTRunDelegateGetDescentCallback,CTRunDelegateGetWidthCallback.我不知道如何使用CTRunDelegateRef我正在创建的对象.现在发生的事情是我的回调没有被调用.

任何有关这方面的指示都将受到高度赞赏.

Thanx提前.

moh*_*enr 11

您应该将运行委托添加为属性字符串中一系列字符的属性.请参阅核心文本字符串属性.绘图时,Core Text将调用您的回调以获取该字符的大小.

更新

这是绘制简单文本的视图的示例代码(请注意,此处没有内存管理代码).

@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
Run Code Online (Sandbox Code Playgroud)

文本中"委托"和"空格"之间的空格字符大小由运行委托控制.