我之所以使用SizeToFit,是因为我不必指定Frame属性才能具有UIButton/UILabel的自适应大小。在我的例子中,它是工具栏上的一个按钮。现在我做了以下观察:
如果我把AdjustsFontSizeToFitWidth和SizeToFit一起使用,字体大小就不再适应了。所以我可以
Frame并使用AdjustsFontSizeToFitWidthSizeToFit,但不再有可调整的字体大小你也有这个问题吗?你如何绕过这个问题?如何让内在内容大小驱动帧大小?还是必须设置Frame属性?Autolayout的优点是不再定义炸薯条的大小.
发布于 2014-09-24 14:54:07
我想我在推理上犯了错误。SizeToFit定义内容之后的大小(内容已经有固定的大小)。AdjustsFontSizeToFitWidth需要一个可以改变字体大小的框架。两个人在一起都没用。
发布于 2018-07-23 06:52:44
下面是我为解决这个问题而做的一个宏,这样您就可以缩小字体大小以适应宽度,并调用sizeToFit,而不需要在收缩后恢复字体大小。
///Works like `adjustsFontSizeToFitWidth` but allows you to use `sizeToFit` and/or measure the new [adjusted] font size; one time adjustment, needs to be called again if `.text` changes.
#define shrinkFontSizeIfNeeded(__label) {\
UIFont *__currentFont = __label.font;\
CGFloat __originalFontSize = __currentFont.pointSize;\
CGSize __currentSize = [__label.text sizeWithAttributes:@{NSFontAttributeName : __currentFont}];\
while (__currentSize.width > __label.frame.size.width && __currentFont.pointSize > (__originalFontSize * __label.minimumScaleFactor)) {\
__currentFont = [__currentFont fontWithSize:__currentFont.pointSize - 1];\
__currentSize = [__label.text sizeWithAttributes:@{NSFontAttributeName : __currentFont}];\
}\
__label.font = __currentFont;\
}https://stackoverflow.com/questions/26019181
复制相似问题