首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AdjustsFontSizeToFitWidth对SizeToFit

AdjustsFontSizeToFitWidth对SizeToFit
EN

Stack Overflow用户
提问于 2014-09-24 14:14:32
回答 2查看 1K关注 0票数 2

我之所以使用SizeToFit,是因为我不必指定Frame属性才能具有UIButton/UILabel的自适应大小。在我的例子中,它是工具栏上的一个按钮。现在我做了以下观察:

如果我把AdjustsFontSizeToFitWidthSizeToFit一起使用,字体大小就不再适应了。所以我可以

  1. 指定Frame并使用AdjustsFontSizeToFitWidth
  2. 使用SizeToFit,但不再有可调整的字体大小

你也有这个问题吗?你如何绕过这个问题?如何让内在内容大小驱动帧大小?还是必须设置Frame属性?Autolayout的优点是不再定义炸薯条的大小.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-24 14:54:07

我想我在推理上犯了错误。SizeToFit定义内容之后的大小(内容已经有固定的大小)。AdjustsFontSizeToFitWidth需要一个可以改变字体大小的框架。两个人在一起都没用。

票数 3
EN

Stack Overflow用户

发布于 2018-07-23 06:52:44

下面是我为解决这个问题而做的一个宏,这样您就可以缩小字体大小以适应宽度,并调用sizeToFit,而不需要在收缩后恢复字体大小。

代码语言:javascript
复制
///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;\
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26019181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档