麻烦左对齐UIButton标题(iOS/Swift)

stu*_*ped 31 title uibutton text-alignment leftalign swift

我有一个问题是左对齐一个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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 107

试试这个:

button.contentHorizontalAlignment = .left
Run Code Online (Sandbox Code Playgroud)

  • .Left已重命名=> .left. (6认同)

iAj*_*iAj 15

更新了Swift 3或Swift 4

yourButton.contentHorizontalAlignment = .left
Run Code Online (Sandbox Code Playgroud)