varun malik
varun malik

Reputation: 23

Label Text Alignment

I have a Label the text set on label is dynamically at run time. My concern is Label is always center of screen. For this I take a label starting point is 0 and width is screenwidth and text alignment center.But I have issue Before starting I adjust a button just before text starting.How we make this button that start exactly before text start. I am not do with code just do in xib. label.textAlignment = NSTextAlignmentCenter

Upvotes: 0

Views: 272

Answers (2)

rushisangani
rushisangani

Reputation: 3395

You can easily do this using autolayout.

enter image description here

Drag an UILabel to xib and set constraints as follows.

1. Set align center X and align center Y to your label.
2. Set trailing space to superview >= 20.
3. Drag a UIButton left to your label and set horizontal spacing between them.
4. Set button's center Y aligned to label's center Y and set button's width to 50.
5. Finally set button's leading space to superview >= 20.

This will adjust your button and label for all iPhones. Don't forget to set numberOfLines to 0.

Upvotes: 1

jigs
jigs

Reputation: 859

You can set button before label dynamically through the below steps:

  1. You have to use sizeToFit property for that label.
  2. Set that label center of the screen.
  3. Get that label's x position using label.frame.origin.x and set that button before that label like label.frame.origin.x-buttonWidth.

Upvotes: 0

Related Questions