简体   繁体   中英

Force text vertically centred in UILabel

I have a UILabel whose size is determined by the distance of the view on top of it and the view at the bottom of it. The height of the UILabel will typically be different depending on the device. It seems that when Auto Layout stretches/crunches the UILabel's height, the text in it clings to the top of the UILabel as if the UILabel had a vertical alignment of text set to top. How can I force the text to be centred vertically?

While not understanding the perks of having Auto Layout stretch UILabel:s, I used an extra UIView in the place of my UILabel, and had Auto Layout stretch that one out instead. Then I put my UILabel centre of that containing UIView. No more constraints used for the UILabel. Seems to work great for now.

Click on the Label.

In the right side pane, select "Attributes Inspector".

For "Baseline" property, choose the option "Align Centers" to make the text centered vertically.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM