最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift按钮的文字对齐不工作

Swift crifan 2954浏览 0评论

通过:

        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按钮的文字对齐不工作

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
87 queries in 0.319 seconds, using 22.11MB memory