现在是否可以使用带有 adjustFontSizeToFitWidth 的 Multiline UILabel? [英] Is it now possible to have Multiline UILabel with adjustsFontSizeToFitWidth?

查看:28
本文介绍了现在是否可以使用带有 adjustFontSizeToFitWidth 的 Multiline UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我一直在努力让 UILabel adjustsFontSizeToFitWidth 处理多行标签.我很快也在

通过在辅助功能检查器上使用最大字体大小的这种配置,我没有这个预期的结果 (1):

但我有这个(2):

可以切换回我的预期结果,将 lineBreakMode 从 NSLineBreakByTruncatingTail 更改为 NSLineBreakByWordWrapping(我在项目中使用的标准设置)

我是不是遗漏了什么?我期望 (1) 来自具有固定宽度和高度的多行标签,并且 adjustsFontSizeToFitWidth 设置为 true 是错误的吗?行为 (2) 是否可靠,我可以指望它吗?我无法从官方文档中理解它.

解决方案

只需要将 minimumScaleFactor/minimumFontSize 设置为与正常字体大小相同(通常默认为标签字体大小的一半)即可得到预期的结果.

I struggled in the past to have UILabel adjustsFontSizeToFitWidth working on multiline labels. I soon have found also here on stackoverflow that

adjustsFontSizeToFitWidth property "is effective only when the numberOfLines property is set to 1".

I remember that this was written in the official documentation. But now I can't find it anymore

I was testing my code on Xcode11 with the accessibility inspector to find out labels that needed to be embedded in scroll views. I found out a label that was showing a adjustsFontSizeToFitWidth behaviour even if it was displaying a multiline text. I inspected the "issue", and I have found out that I had this kind of configuration:

With this configuration using the maximum font size on the accessibility inspector I don't have this expected result (1):

But I instead have this (2):

It is possible to switch back to what was my expected result changing the lineBreakMode from NSLineBreakByTruncatingTail to NSLineBreakByWordWrapping (the standard setup I was using in my project)

Was I missing something? Was I wrong in expecting (1) from a multiline label with fixed width and height and adjustsFontSizeToFitWidth set to true? Is the behaviour (2) reliable, can I count on it? I can't understand it from the official documentation.

解决方案

You need to only set minimumScaleFactor/minimumFontSize to be the same font size as normal(usually its half of the label font size by default) to get the expected result.

这篇关于现在是否可以使用带有 adjustFontSizeToFitWidth 的 Multiline UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆