This is actually rather a difficult problem, because the lines break at word boundaries and word lengths vary, as indeed do character sizes. Plus, you can have an arbitrary number of explicit linebreaks. So to do it properly requires taking into account a lot of the specifics of the text, and a general analytic solution will be, um, quite complicated.
(In "real" typography, there are also a bunch of tricks that are used to fiddle the layout a bit by adjusting the letter and word spacing so that things fit while maintaining a decent appearance, but we're certainly not going to get into that here.)
However, let's put aside exactness and instead consider a simplified model that might help get you into the right ballpark.
The area available is width * height
. Given a piece of text of a given length
, we want to determine a suitable fontsize
such that the wrapped height of the text is close to height
.
For the time being, let's just gloss over all details of units and the vagaries of the text content and imagine that we can calculate the overall size of the text from just length
and fontsize
:
textWidth = A * fontsize * length
lineHeight = B * fontsize
where A
and B
are some scaling constants. (B
should be easy to calculate, and indeed you have it as 1.2. A
is a little trickier and depends on how you choose to measure length
, eg as number of characters or number or words. However you do it, A
can only ever be an approximation, but if you're dealing with reasonable quantities of reasonably well-behaved text then the approximation can be pretty good.)
Then the area occupied by the text is:
textArea = A * fontsize * length * B * fontsize
= A * B * length * fontsize ^ 2
Setting this equal to the available area gives us an upper limit for the fontsize, ie:
fontsize <= sqrt(width * height/(A * B * length))
Clearly this is going to overestimate significantly because it assumes the text packs perfectly, which it doesn't -- there will be area lost at the end of lines and at the bottom of the screen, even in the absence of explicit line breaks. However, it's quite likely that by putting an appropriate fudge factor into our scaling constant A
(ie, by upping our estimated text width to include the lost areas) we can get a good enough fontsize
to be going along with.
We can make the analysis more sophisticated by taking into account more specifics of the text. For example, if you count the number of hard line breaks in the text you can treat those as separate, since they add height without adding width. This gives rise to a slightly more complicated quadratic equation that you'll need the quadratic formula to solve rather just a simple square root. Whether this is worthwhile probably depends mostly on how much variation there is in the rate of explicit linebreaks in the texts you're dealing with.
One other thing to note is that you can often cheat a bit by adjusting only the line spacing, since this allows you to change the vertical span without risking any reflow of the text. You probably don't want to do this by large amounts, because it will look silly. But it's certainly worth considering adding a little bit of vertical padding if you've got the wrapping about right but the text doesn't quite reach the bottom.