在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];

最佳答案

您将颜色设置为titleLabel而不是此颜色,请使用:

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

对于文本对齐,此行将起作用:
toButton.titleLabel.textAlignment = UITextAlignmentRight;

您还可以使用:
toButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

10-06 04:06