【问题标题】:How to set label color and alignment for uibutton dynamically如何动态设置uibutton的标签颜色和对齐方式
【发布时间】:2012-08-23 16:26:41
【问题描述】:

在 xcode 中添加 ui 按钮时,我使用以下代码设置标题标签的文本颜色、对齐方式和字体大小。选择了字体大小,但没有选择文本颜色和对齐方式,为什么?谢谢。

toButton.titleLabel.textColor = [UIColor redColor];
toButton.titleLabel.textAlignment = UITextAlignmentRight;
toButton.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
[toButton setTitle:fromButton.titleLabel.text forState:UIControlStateNormal];

【问题讨论】:

  • 尝试使用设置器而不是属性。 [toButton.titleLabel setTextColor: [UIColor redColor]]
  • @ValeryPavlov 设置器和属性在运行时执行相同的代码,没有区别。

标签: ios dynamic colors uibutton


【解决方案1】:

您将颜色设置为 titleLabel 而不是这个,使用:

[toButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

对于文本对齐,此行将起作用:

toButton.titleLabel.textAlignment = UITextAlignmentRight;

你也可以使用:

toButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

【讨论】:

  • 谢谢,修复了标题颜色。对齐方式呢,有没有设置按钮标签对齐方式的方法?
  • 设置为Button.titleLabel.textAlignment 不起作用。将尝试 contentHorizo​​ntalAlignment。
猜你喜欢
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2015-03-21
  • 2017-07-17
  • 2015-09-14
  • 2012-01-20
  • 1970-01-01
  • 2012-02-11
相关资源
最近更新 更多