Jas Ahluwalia
Jas Ahluwalia

Reputation: 183

vertically center UILabel between two other lables

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!

Upvotes: 1

Views: 564

Answers (2)

rdelmar
rdelmar

Reputation: 104082

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.

Upvotes: 1

bhawesh
bhawesh

Reputation: 1320

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....

Upvotes: 1

Related Questions