标签文本对齐

时间:2016-01-13 07:16:11

标签: ios objective-c uilabel

我有一个标签,标签上的文本集在运行时是动态的。我关心的是Label始终是屏幕的中心。为此,我取一个标签起点为0,宽度为screenwidth和文本对齐中心。但是我有问题在开始之前我在文本开始之前调整一个按钮。我们如何使这个按钮在文本开始之前准确开始。 我不是在xib中做代码。 label.textAlignment = NSTextAlignmentCenter

2 个答案:

答案 0 :(得分:1)

您可以使用自动布局轻松完成此操作。

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.

这将调整所有iPhone的按钮和标签。 别忘了将numberOfLines设置为0。

答案 1 :(得分:0)

您可以通过以下步骤动态设置标签前的按钮:

  1. 您必须使用sizeToFit属性作为该标签。
  2. 设置屏幕的标签中心。
  3. 使用label.frame.origin.x获取该标签的x位置,并在该标签之前设置该按钮,如label.frame.origin.x-buttonWidth