查找最大字符可以适合一行Android JetPack撰写文本
我想找到可以适合一行的字符总数 当前的JetPack组成文本
我可以获取此信息的任何字段。谢谢
I want to find the total number of characters that can fit in one line of a current Jetpack compose Text
Any field where I can get this information. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能在打印文本宽度之前计算文本宽度,请参阅 [1] 或 [2] 。
在这种情况下,我们可以根据需要测量简化的文本,直到获得适合
text
的字符串为止。首先,我们将测量longText
的全长。如果它适合文本
,我们将打印它。如果不合适,我们将减少文本并再次测量。您可以按照自己的意愿进行二进制搜索或按比例分配。如果您有多行文本,并且想适合几行(例如,不超过2行),则可以使用此方法。
只需在
文本
中替换maxlines = 1
,然后添加此功能:而不是接收宽度,而是获得行计数:
并检查它们:
There is a possibility to calculate text width before it will be printed, see [1] or [2].
In this case we can measure a reduced text as many times as we want, until we get a string that will fit inside a
Text
. First we will measure a full length of alongText
. If it fits intoText
, we will print it. If it doesn't fit, we will reduce a text and measure again. You can use binary search or divide proportionally, as you wish.If you have multiline text and want to fit in several lines (for instance, not more than 2 lines), you can use this method.
Just replace
maxLines = 1
inText
, then add this function:Instead of receiving width, get lines count:
And check them:
您可能假设
text()
合并是具有固定尺寸的某些占位符(框)然后您将一些文本放入其中
我们,您可以使用多少个角色来适合这个占位符。
但是事情以其他方式工作:
text()
Composable 没有文本字符串。text()
可综合的占用 尽管文本仅由1个字符组成。
您可能想解决一件事,但请问我们另一件事 - 请参阅什么是XY问题?可能是什么?编辑您的问题。
You probably assume that the
Text()
composable is some placeholder (box) with fixed dimensionsthen you place some text into it
and ask us, how many characters you may use to fit this placeholder.
But things work the other way:
Text()
composable without text string in it.Text()
composable occupy as much space, as it can.So, for example, it occupies the whole screen, if there are no other elements, in spite of the text consist only of 1 character.
So your question in its current form has no sense.
You probably wanted to solve one thing, but ask us about the other – see What is the X-Y problem? and possibly edit your question.