Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For a while I thought this routine was simply broken. It sounds like it's the perfect routine for working out <em>how <strong>high</strong> a piece of text is, when wrapped at a certain width</em>. However this is not what it does. Indeed, the height returned is always a single line. (Which is pointless, incidentally: <code>UIFont</code>'s <code>-leading</code> returns the same value.)</p> <p>To quote the documentation at the time of writing:</p> <blockquote> <p>Although it computes where line breaks would occur, this method does not actually wrap the text to additional lines; [...]</p> </blockquote> <p>This sentence confused me for a long time. My thoughts ran along the lines of:</p> <ul> <li>Computing line-breaks, but not wrapping. Eh?</li> <li>Maybe they mean the receiving <code>NSString</code> itself won't be mutated to reflect line-breaks? But I don't expect that anyway?</li> <li>This sentence is not consistent with itself. I have no choice but to ignore it.</li> </ul> <p>However, it turns out there's a meaning that matches the behaviour we see. It seems that what they mean is:</p> <blockquote> <p>This routine will compute where the <em>first</em> line-break will occur, and all text after this is ignored for the purposes of calculating the bounding box.</p> </blockquote> <p>The bounding box returned will therefore have the height of a single line and a width up to the maximum specified. (The width may be less depending on where the line-break is calculated, or if the text is short enough that it does not need the maximum width.)</p> <p>To, um, answer the actual question: What is it good for? I haven't found anything useful myself: only things where I thought it would be useful, but wasn't. Some things which spring to mind, however, are:</p> <ul> <li>Centering a single-line piece of text. I'm thinking buttons and/or titles here. (Although <code>UILabel</code> and <code>UIButton</code> can do it, sometimes you want to draw it yourself.)</li> <li>Calculating the bounds for an "extract" of a longer piece of text. Here I'm thinking about where you might want to show a snippet of a much larger piece of text.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload