And*_*rew 5 iphone core-graphics objective-c core-text ios
这是我用来在核心图形中显示一些文本的代码:
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];
Run Code Online (Sandbox Code Playgroud)
如果我想在核心文本中显示相同的文本,这是代码:
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);
Run Code Online (Sandbox Code Playgroud)
显然,尽管更加长篇大论,核心文本意味着比核心图形更快,这也是我学会如何做到这一点的原因.但是通过仪器运行此代码,核心图形实现了5ms,而核心文本则在14ms内完成.我觉得我在这里错过了一些东西.drawInRect几乎快了3倍,我听说它应该慢一些.我已经根据我的知识对底部的代码进行了改进,但我不是专家,我很感激任何帮助.
为了澄清,我正在将一段文本绘制到视图上.这就是我想做的一切.并且所有文本都具有相同的外观.
我不使用 CT 的东西,但在我看来,你在每次绘制时都设置了不变的字体和其他属性。尝试分解注释“翻转坐标系,并缓存 attString”之前的所有内容。
这应该会提高速度,以便更好地进行面对面测试。
| 归档时间: |
|
| 查看次数: |
2257 次 |
| 最近记录: |