旅游小网站怎样做精不做全,组建网站 多少钱,东昌府聊城网站优化,信息门户网站怎么做设计师给到的图#xff0c;经常是 “按钮的左边距离文字右边5px” 这样子的标注。于是我们需要计算文字的宽度。 有两种方法#xff1a; 用 label 或 button 的 sizetofit 方法 [label sizeToFit];
NSLog(label size is %,[NSValue valueWithCGSize:label.boun…设计师给到的图经常是 “按钮的左边距离文字右边5px” 这样子的标注。于是我们需要计算文字的宽度。 有两种方法 用 label 或 button 的 sizetofit 方法 [label sizeToFit];
NSLog(label size is %,[NSValue valueWithCGSize:label.bounds.size]);
用 sizeWithAttributes方法 CGSize size [label.text sizeWithAttributes:{NSFontAttributeName:label.font}];
NSLog(size WithAttributes is %,[NSValue valueWithCGSize:size]);
结果如何呢 label size is NSSize: {96.5, 20.5} size WithAttributes is NSSize: {96.39697265625, 20.287109375} 可见两个返回值不同第二种方法返回的值似乎精确应该用哪个呢 我们来看下文档怎么说的。 关于 sizeWithAttributes 的文档 This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function. 关于 sizeToFit 的文档 Call this method when you want to resize the current view so that it uses the most appropriate amount of space. 在文档中明确指出了使用 sizeWithAttributes 时需要使用 ceil 方法取整。而 sizeToFit 方法会当前 view 的大小调整为 appropriate 大小。 这里的关键是对 appropriate 的理解。我说下我的理解至少应该满足两个条件 不截断 就是说返回的值应该不小于文字的宽度/长度否则会被截断。像素对齐 所有的 view 的起始和结束为止都应该在物理像素上。因此在 2x 屏幕应该返回 0.5 的整数倍在 1x 屏幕上应该返回 1 的整数倍。因此应该用第二种方法或者把第一种方法的结果向上取整。 如果直接使用第一种方法的结果会导致其他界面像素对不齐有奇怪的表现。 想象一下计算机如何处理 “一条线段起点是 3.452 像素终点是 56.341 像素” 这种奇怪的东西。 转载于:https://www.cnblogs.com/huahuahu/p/iOS-ji-suan-wen-zi-kuan-du-de-yi-ge-xi-jie.html