Tuesday, 3 September 2013

Where is this vertical spacing coming from in UILabelView?

Where is this vertical spacing coming from in UILabelView?

I'm creating an iOS view that displays various static text elements. The
xib looks like this:

It uses four labels for the title, timestamp, body, and footer. Every view
is anchored to the sibling view above it vertically and anchored to the
left/right of the parent view. All labels have a fixed height except the
body which has a >= height and the number of lines set to 0 with "word
wrap" as the line wrapping style. The parent view is a UIScrollView.
On the iPhone it looks like fine:

However on the iPad it looks like this:

Huh? Where is all that extra vertical space in the body label coming from?
The xib and its view controller are identical between iPhone and iPad
(there is no custom iPad code at the moment). I've found that the vertical
space is directly related to how many line-wraps the label renders. If no
lines wrap, no extra vertical space. If only a few lines wrap, there's a
little extra vertical space. If nearly every line wraps, well, that's what
it looks like.
First of all any ideas on why UILabel is behaving this way?
Second of all, if I can't make it stop doing this how can I work around it?
I've already tried a few things. If I call [bodyLabel sizeToFit] within
-viewDidLayoutSubViews then it fixes the label but doesn't fix the layout
of any of the sibling views (e.g. the Footer label is stuck way at the
bottom of the screen instead of pulled up to just under the body). Any
attempts to get the entire view to re-layout its children after calling
sizeToFit is ignored. I've also tried sizing the UILabel by calculating
height based on font, which results in the same behavior as -sizeToFit
(albeit with more code).
Replacing the Body UILabel with a UITextView instead doesn't give me the
weird vertical spacing issues but I need to calculate the height of the
UITextView manually (using font calculations) and something about resizing
the UITextView within the parent UIScrollView makes it so the UIScrollView
simply refuses to scroll (as if it doesn't know its contents are too big
for its bounds).
So at the moment I'm stuck. Even just an explanation of why UILabel
behaves this way on the iPad layout would be helpful.

No comments:

Post a Comment