如何在iPad中使用CTRunDelegate?
我正在开发一个iPad应用程序,在其中我必须使用如何在iPad中使用CTRunDelegate?,ipad,ios,core-text,Ipad,Ios,Core Text,我正在开发一个iPad应用程序,在其中我必须使用Ctrunelegate。我已经定义了所需的所有回调,即CTRunDelegateGetAscentCallback,CTRunDelegateGetDescentCallback,CTRunDelegateGetWidthCallback。我不知道如何使用我正在创建的对象。现在发生的事情是,我的回调没有被调用 在这方面的任何建议都将受到高度赞赏 Thanx提前。您应该将运行代理添加为属性字符串中一系列字符的属性。看见绘制时,核心文本将调用回调以获
Ctrunelegate
。我已经定义了所需的所有回调,即CTRunDelegateGetAscentCallback
,CTRunDelegateGetDescentCallback
,CTRunDelegateGetWidthCallback
。我不知道如何使用我正在创建的对象。现在发生的事情是,我的回调没有被调用
在这方面的任何建议都将受到高度赞赏
Thanx提前。您应该将运行代理添加为属性字符串中一系列字符的属性。看见绘制时,核心文本将调用回调以获取该字符的大小 更新 这是一个简单文本视图的示例代码(请注意,这里没有内存管理代码)
文本中“delegate”和“space”之间空格字符的大小由run delegate控制。我已经这样做了,但没有帮助。文档本身并没有提供关于CTRunDelegate的很多信息。如果你有CTRunDelegate的任何示例代码,那将是非常有帮助的。我在看到你的答案之前就得到了答案,但你为给我正确的答案付出了努力,所以这笔奖金归你所有。谢谢你的回复。嗨,莫,我的申请还需要你的帮助。你有兴趣帮我吗??
@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