[已解决]swift给按钮添加文字

代码:

    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 add text for button

 

搜:

swift button text

参考:

去试试:
set title for button
搜:
        //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等的时候的字体和字体颜色:


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量