38

我在左对齐 aUIButton的文本时遇到问题。我也尝试将其更改为,.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)

在此处输入图像描述

4

2 回答 2

147

尝试这个:

button.contentHorizontalAlignment = .left
于 2016-04-07T00:48:58.007 回答
19

为 Swift 3 或 Swift 4 更新

yourButton.contentHorizontalAlignment = .left
于 2017-08-09T09:29:40.210 回答