通过:
|         forgotPwdButton.frame = CGRectMake(             loginButton.frame.origin.x,             registerButton.frame.origin.y,             140,             registerButton.frame.height)         forgotPwdButton.setTitle("忘记密码?", forState: UIControlState.Normal)         forgotPwdButton.backgroundColor = UIColor.clearColor()         forgotPwdButton.titleLabel?.font = UIFont.systemFontOfSize(14)         forgotPwdButton.titleLabel?.textAlignment = NSTextAlignment.Left         forgotPwdButton.setTitleColor(ColorButtonBackgroudDarkBlue, forState: UIControlState.Normal)         forgotPwdButton.addTarget(self, action: Selector("forgotPassword"), forControlEvents: UIControlEvents.TouchUpInside)         //for debug         forgotPwdButton.backgroundColor = UIColor.greenColor() | 
设置了按钮forgotPwdButton的文字的左对齐,但是实际上不起效果:

swift button text alignment
Button Title Alignment in Swift – Stack Overflow
设置UIButton的文字显示位置、字体的大小、字体的颜色 – 承影 – 博客频道 – CSDN.NET
| //        forgotPwdButton.titleLabel?.textAlignment = NSTextAlignment.Left         forgotPwdButton.contentHorizontalAlignment = .Left | 
就可以了:

[总结]
swift中,对于Button按钮来说,想要设置标题文字的对齐方式,用contentHorizontalAlignment,比如:
| forgotPwdButton.contentHorizontalAlignment = .Left | 
即可。
转载请注明:在路上 » [已解决]swift按钮的文字对齐不工作