vertically center UILabel between two other lables

有些话、适合烂在心里 提交于 2020-01-16 02:51:23

问题


I have 3 UILabels drawn in storyboard positioned on top of each other.

Static UI Label 1

Dynamic UI Label 2

Static UI Label 3

Labels 1 and 3 are static and never change. Label 2 is dynamic and is always one sentence long, but could be a short or long sentence that wraps. I want Label 2 to be perfectly vertically centered between label 1 and label 3 based on how much text is there. Any ideas how to do this? Greatly appreciated!


回答1:


If you're using auto layout (which is on by default), then you can just stretch the middle label until it's top and bottom are the standard distance away from the other two labels (you will see a dotted blue line when you reach that distance). This assumes that your label has a clear background, or that you don't mind seeing a tall label if it doesn't. The text will be centered vertically in this tall label regardless of the number of lines. It will also stay centered on rotation.




回答2:


Add the center values of Label1 and Label3 , Divide it by 2 and make it center of label2

CGPoint point = CGPointMake(Label2.Center.x,(Label1.center.y+Label3.center.y)/2);
Label2.center = point;

That's all....



来源:https://stackoverflow.com/questions/15770875/vertically-center-uilabel-between-two-other-lables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!