【问题标题】:Change the color and alignment of a UIButton's text更改 UIButton 文本的颜色和对齐方式
【发布时间】:2012-08-31 23:24:45
【问题描述】:

我正在UITableView 中创建UIButton

UIButton *aboutButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[aboutButton setTitle:@"ABOUT" forState:UIControlStateNormal];
[aboutButton addTarget:self action:@selector(aboutButtonClicked) forControlEvents: UIControlEventTouchUpInside];
aboutButton.frame=CGRectMake(20, 375, 200, 35);
[tableView addSubView aboutButton];

按钮工作正常......但按钮文本为蓝色且居中对齐。我想更改文本颜色和对齐方式 - 我该怎么做?

【问题讨论】:

  • 这真的应该可以通过点击查看UIButtonUILabel 类引用来解决,没有必要在SO上发布这个。您可以在 Xcode 或网络上查看文档,它们非常简单明了。

标签: iphone ios ipad uibutton


【解决方案1】:

设置按钮标题颜色和对齐方式...

[aboutButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter]; 

aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;

[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

【讨论】:

  • [aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter];不工作....
  • 使用此代码 [aboutButton setContentHorizo​​ntalAlignment:UIControlContentHorizo​​ntalAlignmentRight];
  • aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;正在工作
  • 默认情况下它是居中对齐的...但是如果我使用 aboutButton.titleLabel.textAlignment = UITextAlignmentLeft,它仍然是居中的..
  • [关于按钮 setContentHorizo​​ntalAlignment:UIControlContentHorizo​​ntalAlignmentRight];
【解决方案2】:
[aboutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

在 SO 中已被多次询问

button alignment question

another button alignment question

button title color question

another button title color question

【讨论】:

    【解决方案3】:

    您使用-[myButton setTitleColor:forState:] 设置标题的颜色。 可以使用[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]更改对齐方式

    【讨论】:

    • 可以设置按钮边框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多