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

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

Swift crifan 3228浏览 0评论

代码:

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

 

搜:

swift button text

参考:

ios – How to change button text in Swift Xcode 6? – Stack Overflow
去试试:
set title for button
搜:
后来的完整代码是:
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给按钮添加文字

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.387 seconds, using 22.16MB memory