NSString boundingRectWithSize边界不被尊重

前端之家收集整理的这篇文章主要介绍了NSString boundingRectWithSize边界不被尊重前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人知道在这里做的工作,或者我目前在做错什么.
CGSize boundingSize = CGSizeMake(288,9999);

CGRect boundingRect = [text boundingRectWithSize:boundingSize 
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:[NSDictionary dictionaryWithObjectsAndKeys:theFont,NSFontAttributeName,nil]
                                                 context:nil];

代码在iOS7上运行(手机和SIM卡)时,返回的尺寸为416.3宽,高度为15.5.显然,boundingRectWithSize被忽略.有其他人遇到这个问题,如果有的话有任何人有关于如何解决这个问题的想法,还是有人指出我到哪去错了.

解决方法

根据 Apple documentation

You can use this method to compute the space required to draw the
string. The constraints you specify in the size parameter are a guide
for the renderer for how to size the string. However,the actual
bounding rectangle returned by this method can be larger than the
constraints if additional space is needed to render the entire string.
Typically,the renderer preserves the width constraint and adjusts the
height constraint as needed.

如果你指定一个固定的字体和太小的空间,一些东西必须给.在这种情况下,它是有界的空间.我猜你预期换行.宽度288允许任何合理的包装?

猜你在找的iOS相关文章