How do I use CtrunDelegate in the iPad?

I am developing an iPad application and I have to use CTRunDelegate. I have defined all the required callbacks, namely CTRunDelegateGetAscentCallback, CTRunDelegateGetDescentCallback, CTRunDelegateGetWidthCallback. I don’t know how to use the CTRunDelegateRef I’m creating Object. What is happening now is that my callback is not being called.

Any instructions on this will be highly appreciated.

Complete ahead of time.

You should add the run delegate as an attribute of a series of characters in the attribute string. See Core Text String Attributes. When drawing, Core Text will call your callback to get the size of the character.

Update

This is the sample code for drawing a simple text view (please note that this There is no memory management code).

@implementation View

/* Callbacks */
void MyDeallocationCallback( void* refCon ){< br />
}
CGFloat MyGetAscentCallback( void *refCon ){
return 10.0;
}
CGFloat MyGetDescentCallback( void *refCon ){
return 4.0 ;
}
CGFloat MyGetWidthCallback( void* refCon ){
return 125;
}

- (void)drawRect:(CGRect)rect {< br /> // create an attributed string
NSMutableAttributedString * attrString = [[NSMutableAttribu tedString 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);< br />
// 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 = CTFramesetterCre ateFrame(frameSetter, CFRangeMake(0, attrString.length), path, NULL);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextSetTextPosition(context, 0.0, 0.0);
CTFrameDraw(frame, context);
}

@end

The space character between “delegation” and “space” in the text The size is controlled by the run delegate.

I am developing an iPad app and I have to use CTRunDelegate. I have defined all the required callbacks, namely CTRunDelegateGetAscentCallback, CTRunDelegateGetDescentCallback, CTRunDelegateGetWidthCallback. I don’t know how to use the CTRunDelegateRef object I am creating. What is happening now is that my callback is not being called.

Any pointers on this will be highly appreciated.

Complete ahead of time.

You should add the run delegate as an attribute of a series of characters in the attribute string. Please refer to Core Text String Attributes. When drawing , Core Text will call your callback to get the size of the character.

Update

This is the sample code for drawing a simple text view (please note that there is no Memory management code).

@implementation View

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

}
CGFloat MyGetAscentCallback( void *refCon ){
return 10.0;
}
CGFloat MyGetD escentCallback( 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);

// cre ate 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

Between “delegation” and “space” in the text The size of the space character is controlled by the running delegate.

Leave a Comment

Your email address will not be published.