tags:

views:

102

answers:

2

I am a developing an iPad application in which i have to use CTRunDelegate. Ihave defined all the the callbacks that are required viz CTRunDelegateGetAscentCallback , CTRunDelegateGetDescentCallback , CTRunDelegateGetWidthCallback. I dont know how to use CTRunDelegateRef object that I am creating. Right now what is happening is that my callbacks are not getting called.

Any pointers in this regard will be highly appreciated.

Thanx in advance.

+1  A: 

You should add your run delegate as an attribute for a range of characters in your attributed string. See Core Text String Attributes. When drawing, Core Text will call your callbacks to get the sizing of that characters.

Update

This is a sample code for a view drawing a simple text (Note that there's no memory management code here).

@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

The size of the space character between "delegate" and "space" in the text are controlled by the run delegate.

Mo
I already did that but it is not helping. The documentation itself doesn't give much info about CTRunDelegate. if you have any sample code for CTRunDelegate , that will be of great help.
tek3
I got the answer before I saw your answer but you made effort for giving me the right answer , so this bounty goes to you. Thanx a lot for your response.
tek3
Hi Mo , I need some more help from you for my application. Will you be interested to help me out??
tek3
A: 

We have tried the above given code snippet and successfully able to have a width as is returned by the callback

CGFloat MyGetWidthCallback( void* refCon ){
    return 125;
}

Also the callbacks for Ascent and Descent are being called but these are not reflected in the resulting attributed string. Is this a bug or does the above code is missing something?

Parvez Qureshi