https://kotlinlang.org logo
Title
d

David Herman

05/02/2022, 6:24 AM
I have a window in which I will render a bunch of text with monospace font. What I'd like to do is initially size the window to some width by height as decided by the font, e.g. 80x50 means a window that can fit 80 characters wide and 50 lines tall. Is there any trivial way to check with size of text rendering without first creating a window?
I did some initial research into how to measure layouts, but that doesn't seem relevant here, as I have the chicken vs. egg problem of needing a Window in order to render layouts that I want to measure... I think?
I saw that Windows allow you to pass in Dp.Unspecified as a way to auto-measure its size, so I was thinking of using that by doing an initial dummy render, but that didn't seem to work. (I used a "firstFrame" boolean, rendering the dummy text area, then set "firstFrame" to false, but it seems that that happened so quickly that the window, when measuring itself, used the composables rendering in a later frame). Anyway, that also seems like a hack...
What I'm looking for I guess is a Composable that I can call within an
application
that can take a font, some text, and give me the size of that text in
sp
or
dp
units.
Nevermind, I think I figured out how to do it with
SubcomposeLayout
based on this answer https://stackoverflow.com/a/70508246/17966710 in combination with Dp.Unspecified.
👍🏿 1