iphone - Core Graphics 文本比 Core Text 快得多——我觉得我错过了什么

标签 iphone objective-c ios core-graphics core-text

这是我用来在核心图形中显示一些文本的代码:

int y = MESSAGE_FRAME.origin.y + 8;

if (month) y = y + 27;
int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];

CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);

UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];

[[UIColor colorWithWhite:0.2 alpha:1.0] setFill];

[self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

如果我想在核心文本中显示相同的文本,这里是代码:

CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height + 2, MESSAGE_FRAME.size.width - 16, height);


    UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];
    CGFloat lineHeight = [font lineHeight];

    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL);

    CGFloat lineSpacing = 0;

    CTParagraphStyleSetting settings[] = {
        { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(lineHeight), &lineHeight },
        { kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(lineSpacing), &lineSpacing },

    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));

    NSMutableDictionary *attDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   (__bridge_transfer id)fontRef, (NSString *)kCTFontAttributeName,
                                          (id)[[UIColor colorWithWhite:0.2 alpha:1.0] CGColor], (NSString *)kCTForegroundColorAttributeName,
                                          paragraphStyle, kCTParagraphStyleAttributeName,
                                          nil];

     NSAttributedString *attString = [[NSAttributedString alloc] initWithString:self.entry.message attributes:attDictionary];

    //Flip the coordinate system
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);



    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect2);


    self.framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attString);
    CTFrameRef theFrame = CTFramesetterCreateFrame(self.framesetter, CFRangeMake(0, [attString length]), path, NULL);

    CFRelease(path);

    CTFrameDraw(theFrame, context);
    CFRelease(theFrame);

显然,尽管内容更冗长,但核心文本应该比核心图形更快,这就是我学习如何做的原因。但是在仪器中运行这段代码,核心图形达到了 5 毫秒,而核心文本达到了 14 毫秒。我觉得我在这里遗漏了一些东西。 drawInRect 几乎快了 3 倍,我听说它应该更慢。我已经尽我所能完善了底部的代码,但我不是专家,如果有任何帮助,我将不胜感激。

澄清一下,我在 View 上绘制了一个文本 block 。这就是我想要做的。并且所有的文本都具有相同的外观。

最佳答案

我不使用 CT 的东西,但在我看来你在每次绘制时都设置了不变的字体和其他属性。尝试分解出注释“翻转坐标系,并缓存 attString”之前的所有内容。

这应该会提高速度,以实现更好的面对面测试。

关于iphone - Core Graphics 文本比 Core Text 快得多——我觉得我错过了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10611459/

相关文章:

iphone - 应用商店首页图片

iphone - 是否可以在 iPhone 操作系统或 Android 中检测 DPI?

ios - 重新安装后删除 iOS 推送通知

ios - 导航栏返回按钮方法

ios - 从通知中心移除远程通知

ios - 如何在给定第二个数组的情况下找到某个值的位置?

iphone - 核心数据关系困惑

iphone - 使用游戏中心向所有玩家显示相同的牌

objective-c - 接受 iPhoto 或 Aperture 的拖放

ios - 我如何实现 `prepareForReuse` ?