boundingRectWithSize计算文字高度不准问题

在项目中遇到了计算label高度不对的情况,参照https://www.jianshu.com/p/c615a76dace2
我自己试验了下,\r\n这种不会导致计算错误,重要的是boundingRectWithSize方法官方注释就说了:
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.
In iOS 7 and later, this method returns fractional sizes (in the size component of the returned rectangle); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
翻译:
可以使用此方法计算绘制字符串所需的空间。在size参数中指定的约束是呈现器如何调整字符串大小的指南。但是,如果需要额外的空间来呈现整个字符串,则此方法返回的实际边框可能比约束更大。通常,渲染器保留宽度约束,并根据需要调整高度约束。

在ios7及以后版本中,该方法返回小数大小(在返回矩形的大小组件中);要使用返回的size to size视图,必须使用ceil函数将其值提升到最近的整数。

即,用ceil将小数向上取整来解决问题

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我把生命带来 种在不向阳的土里 它在大雨下颠沛流离 它被风霜凌迟 它有心挣扎却无力逃跑 萎靡开始亲近它 稀释的土壤...
    夕木想做梦阅读 180评论 0 0
  • 在这个信息泛滥的时代,写文章是悦己还是悦人,每个人都有着自己的选择。还是喜欢直抒胸臆的文字,用文字去观人生,看世界...
    北回归雁阅读 259评论 0 0
  • 2018年10月20日 星期六 再一次体验到了考试的艰辛, 再一次体验到了不学习去考试的艰辛 睡眠严重不足 不管考...
    晴致生活馆阅读 221评论 0 0
  • 关于控制与爱。 关注事情是为了控制,关注心情是为了爱。人们很容易把注意力放在关注事情上,因为事情通常在行为层面,是...
    阅己阅人阅读 267评论 0 5
  • 通常在Python语言中我们判断两个字符串是否相等的时候,很多同学在初学Python的时候会混用==和is,最后造...
    妄心xyx阅读 604评论 0 1