HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在iPad中使用CTRunDelegate?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个iPad应用程序,我必须使用CTRunDelegate.我已经定义了所有需要的回调,即CTRunDelegateGetAscentCallback,CTRunDelegateGetDescentCallback,CTRunDelegateGetWidthCallback.我不知道如何使用我正在创建的CTRunDelegateRef对象.现在发生的事情是我的回调没有被调用.

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

提前完成.

解决方法

您应该将运行委托添加属性字符串中一系列字符的属性.请参阅 Core Text String Attributes.绘图时,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

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

大佬总结

以上是大佬教程为你收集整理的如何在iPad中使用CTRunDelegate?全部内容,希望文章能够帮你解决如何在iPad中使用CTRunDelegate?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: