我正在使用 UIKit 中的 drawInRect 来绘制一个字符串。我还想绘制(查看)绘制字符串的边界矩形(有点像在盒子里有一个字符串)。这该怎么做?
问问题
258 次
2 回答
2
drawInRect
UIKit 方法返回 a CGSize
,它是绘制字符串的大小。CGRect
将此与您传递给的原点一起使用drawInRect
,这就是您要绘制的矩形。
CGSize size = [string drawInRect:rect .... plus your options];
CGRect boundingRect = rect;
boundingRect.size = size;
[[UIBezierPath bezierPathWithRect:boundingRect] stroke];
于 2011-09-14T10:55:23.820 回答
0
drawinRect 不再返回 CGSize ,所以根据 jrturton 的帖子,我使用类似这样的东西来得到一个完全围绕字符串内容绘制的框 -
[str1 drawInRect:rect withAttributes:attributes];
CGRect boundingRect = [str1 boundingRectWithSize:rect.size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
boundingRect.origin.x = rect.origin.x;
boundingRect.origin.y = rect.origin.y;
[[UIBezierPath bezierPathWithRect:boundingRect] stroke];
于 2015-05-29T05:29:07.720 回答