Search code examples
iphoneinterface-builderpositioning

Anchor UIButton to the left of a UILabel


I have a UILabel with right-aligned text that may vary from between three to seven characters in length. I am trying to anchor a button to the left of this text so that when the length of the text increases, the button will stay the same distance from the left of the label.

Is there an anchor setting in Interface Builder, or does it require a code-level solution (e.g. setting the button's x position according to the width of the label)?


Solution

  • You need to do this in code. Interface builder is not build for runtime calculations.

    To calculate the width of your text you can use NSString's sizeWithFont: method.