如何在iPad中使用CTRunDelegate

如何在iPad中使用CTRunDelegate

本文介绍了如何在iPad中使用CTRunDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a developing an iPad application in which i have to use CTRunDelegate. I have 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.

提前完成。

推荐答案

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

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.

更新

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

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.

这篇关于如何在iPad中使用CTRunDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:02