Frage

this question was asked several times in different contexts but I didn't find a SO Question that solved my specific problem. This one here is the one that asks the same but the answers are "Non-Autolayout"-conform.

How to align baselines of text in UILabels with different font sizes on iOS?

I layouted my UILabels and one UIImageView with NSLayoutConstraints so that they are bottom aligned (like you see it in the screenshot)

enter image description here

The UILabels and the UIImageView have no defined height or width, they automatically adjust their bounds depending on the content. But you can see that the "9" and the "%" aren't on the same baseline, even if the labels itselves are bottom aligned. This is because of the different font size.

Methods like sizeToFit won't work, because the Labels are already sized by Autolayout so that the content fits automatically.

How can I achieve the bottom alignment with different font sizes? I would prefer no hacks in layoutSubviews and other stuff... I like the way how Autolayout works for me but I can't understand why we don't have NSTextAlignmentBottom and NSTextAlignmentTop in 2013...

Any suggestions or hints? Thanks!

War es hilfreich?

Lösung

Have you used NSLayoutFormatAlignAllBaseline? Here's an example of how it lays out UILabels:

enter image description here

I've made this example in the Interface Builder, but it's the same when set up programmatically.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top