【发布时间】:2012-03-21 06:20:34
【问题描述】:
乍一看,我的问题看起来很简单,但似乎我真的找不到解决方案。 这是什么: 我想计算 CATextLayer 字符串的边界框。 这是我的工作:
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = CGRectMake(80, 0.0f, 36.0f, 18.0f);
textLayer.string = @"12";
textLayer.fontSize = [UIFont systemFontSize];
textLayer.foregroundColor = [UIColor whiteColor].CGColor;
NSLog(@"(width,height)=(%f,%f)",
[textLayer.string sizeWithFont:textLayer.font].width,
[textLayer.string sizeWithFont:textLayer.font].height);
问题是输出总是:(width,height) = (8.000000,0.000000)
【问题讨论】:
-
Swift 用户,如果只使用一行(可能更多),请尝试 myCATextLayer.preferredFrameSize() 快速获胜。
标签: iphone objective-c ios cocoa-touch calayer