最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

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

Swift crifan 9浏览 0评论

代码:

    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

参考:

ios – How to change button text in Swift Xcode 6? – Stack Overflow
去试试:
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等的时候的字体和字体颜色:

转载请注明:在路上 » [已解决]swift给按钮添加文字

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
55 queries in 0.533 seconds, using 12.40MB memory