ios - 如何使 UIButton 文本对齐中心?使用 IB

标签 ios objective-c uibutton interface-builder

我无法以 IB 为中心设置 UIButton 的标题。我的标题是多行的。
就是这样一个
enter image description here

但是我想要这个
enter image description here

我在这方面给了空间,但我不想那样做。因为在某些情况下它并没有完全对齐,我知道 UILabel 有一个属性可以设置对齐,但我不想为此编写代码.. 只想从 IB 设置所有内容。

谢谢

最佳答案

这将完全符合您的预期:

objective-C :

 [myButton.titleLabel setTextAlignment:UITextAlignmentCenter];

对于 iOS 6 或更高版本是

 [myButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

tyler53's answer 中所述

swift :

myButton.titleLabel?.textAlignment = NSTextAlignment.Center

Swift 4.x 及以上版本

myButton.titleLabel?.textAlignment = .center

关于ios - 如何使 UIButton 文本对齐中心?使用 IB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5712937/

相关文章:

ios - 在 Swift 中以编程方式制作 Segue

ios - 为什么在这段代码中我会得到 "Unexpected non-void return value in void function"

ios - 远程服务器不可用时的本地通知

ios - swift 中的 CDPieMenu (cocoapod)

ios - UIAlertController 不是窗口层次结构

swift - 重新加载 UIWebView 的按钮

ios - 数组最后插入/删除的项目

ios - StatusBar 不遵守应用程序的方向和旋转

ios - 更改所有 UIButton 字体

ios - UIStackView 按钮中的 UIButton 单击不起作用