UIButton 标题左对齐问题 (iOS/Swift)
Posted
技术标签:
【中文标题】UIButton 标题左对齐问题 (iOS/Swift)【英文标题】:Trouble left-aligning UIButton title (iOS/Swift) 【发布时间】:2016-07-27 15:46:59 【问题描述】:我在左对齐UIButton
的文本时遇到问题。我也尝试将其更改为.Right
,但它仍然保持居中。我还尝试了aButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
而不是aButton.titleLabel?.textAlignment = .Left
,但这也没有改变任何东西。是否有其他方法可以以编程方式更改 UIButton
标题的对齐方式?
allButtonViews = UIView(frame: CGRectMake(0, 44, 100, 100))
allButtonViews.backgroundColor = .redColor()
let campusButton = UIButton(type: UIButtonType.System) as UIButton
aButton.frame = CGRectMake(0, 0, 300, 70)
aButton.setTitle("A", forState: .Normal)
aButton.titleLabel?.textColor = .blueColor()
aButton.titleLabel?.textAlignment = .Left
aButton.backgroundColor = .whiteColor()
sortView.addSubview(aButton)
view.addSubview(allButtonViews)
【问题讨论】:
【参考方案1】:试试这个:
button.contentHorizontalAlignment = .left
【讨论】:
Xcode 中是否有任何设置可以启用相同的功能? 添加一个用户定义的运行时属性,其中 contentHorizontalAlignment 作为 NSNumber,中心、左、右和填充的值为 0-3。 .Left 已重命名 => .left.【参考方案2】:为 Swift 3 或 Swift 4 更新
yourButton.contentHorizontalAlignment = .left
【讨论】:
以上是关于UIButton 标题左对齐问题 (iOS/Swift)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIButton 的右边缘对齐 UIButton 图像