How to constrain a view to a UITextView's first baseline?
Asked Answered
I

0

11

I'm desperately trying to constrain the first baselines of a UILabel and a UITextView in Interface Builder. From my understanding this should be quite easy by simply adding the following constraint (Pseudo-Code):

label.firstBaseline = textView.firstBaseline

However, depending on the other constraints and some view settings I get really awkward results as shown in the following screenshots.

1st Case: Disable scrolling & Align the top edges

This is only for showing the general view setup. I have disabled scrolling for the textView to avoid ambiguity. (Otherwise the textView cannot calculate its own height from its containing text.)

1st case: Align top edges of both views

2nd Case: Disable scrolling & align the first baselines

Same setup as in the 1st case with the only difference that I have removed the constraint that aligns the top edges of the views and added a constraint that aligns the first baselines of the views instead.

For some reason, the label "takes off" well beyond the bounds of its superview and ends up at a y position of 7764. No idea why.

2nd case: Align first baselines

3rd Case: Constrain height & align the first baselines

Now I have scrolling enabled for the textView and constrained its height instead.

Obviously the label's first baseline gets aligned with the textViews top edge even though the constrained says to align both their first baselines. I read in the Apple docs that the firstBaseline parameter returns a view's top edge if that view doesn't have a baseline. So it seems that the textView doesn't have a first baseline in this scenario.

3rd case: Align first baselines and add height constraint

Does a UITextView in general have no firstBaseline? And if so: Why? After all, it's a view that draws text and thus this property should be set.

Any thoughts on this are appreciated.

Imminence answered 25/8, 2016 at 10:55 Comment(3)
have you found a solution for this issue?Dalesman
No, unfortunately not.Imminence
I ended with adding a hidden dummy label with the same font as the text view (TV), aligning it to the element that I wanted the TV to be aligned to, and the TV is aligned to the top of the label (with offset of textView.contentInset.top + textView.textContainerInset.top)..Dalesman

© 2022 - 2024 — McMap. All rights reserved.