代码:
1 2 3 4 5 6 7 8 | var loginButton:UIButton = UIButton(type: UIButtonType.RoundedRect) loginButton.frame = CGRectMake( 0, passwordTextField.frame.origin.y + passwordTextField.frame.height + textFieldPaddingY*2, passwordTextField.frame.width - 40, usernameTextField.frame.height) |
想要添加一个文字,变成:
搜:
swift button text
参考:
去试试:

搜:
后来的完整代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //5. login button loginButton.frame = CGRectMake( 0, passwordTextField.frame.origin.y + passwordTextField.frame.height + textFieldPaddingY*2, passwordTextField.frame.width - 40, usernameTextField.frame.height) loginButton.center.x = screenCenterXPos loginButton.backgroundColor = UIColor(hexString: "#249ed8" ) loginButton.tintColor = UIColor.whiteColor() loginButton.titleLabel?.font = UIFont.systemFontOfSize(16) loginButton.setTitle( "登录" , forState: UIControlState.Normal) loginButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) loginButton.addTarget(self, action: Selector( "loginAction" ), forControlEvents: UIControlEvents.TouchUpInside) loginButton.layer.cornerRadius = 16 |
效果:

即:
给按钮设置文字内容和文字颜色的话,用:
setTitle
setTitleColor
并且,可以设置,button在不同的state,比如normal,disabled等的时候的字体和字体颜色:
转载请注明:在路上 » [已解决]swift给按钮添加文字