[英]Cannot change textColor and font size inside the loop in swift
I have no idea why the font size and text Color doesn't takes any effect in the loop...anyone help?? 我不知道为什么字体大小和文本颜色在循环中不起作用...有人帮忙吗? Here is my code: 这是我的代码:
var buttons = ["N","F","S","D","C","A","R"]
for button in buttons
{
var Button:UIButton = UIButton()
Button = UIButton(frame: CGRect(x:10, y: 20, width: 80, height: 80))
Button.frame.origin = ButtonPosition
ButtonPosition.x = ButtonPosition.x + buttonIncrement
Button.layer.cornerRadius = 3
Button.clipsToBounds = true
Button.backgroundColor = UIColor.groupTableViewBackgroundColor()
Button.setTitle("\(button)", forState: UIControlState.Normal)
Button.titleLabel?.text = "\(button)"
Button.titleLabel?.font = UIFont(name:"Chalkboard SE Regular", size: 30)
Button.titleLabel?.textColor = UIColor.blackColor()
Button.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#b8b4af", alpha: 0.5)), forState: .Highlighted)
Button.addTarget(self, action: "check:", forControlEvents: UIControlEvents.TouchUpInside)
buttonView.addSubview(Button)
}
return buttonView
You should edit directly the button, not the titleLabel
, like this: 您应该直接编辑按钮,而不是titleLabel
,如下所示:
Button.setTitle("\(button)", forState: .Normal)
Button.setTitleColor(UIColor.blackColor(), forState: .Normal)
First remove the line Button.titleLabel?.text = "\\(button)"
. 首先删除行Button.titleLabel?.text = "\\(button)"
。 This one is enough Button.setTitle("\\(button)", forState: UIControlState.Normal)
. 这是足够的Button.setTitle("\\(button)", forState: UIControlState.Normal)
。
Then you should use setTitleColor
method from a button to change it. 然后,您应该通过按钮使用setTitleColor
方法进行更改。 Button.setTitleColor(UIColor. blackColor(), forState: UIControlState.Normal)
Finally you made a mistake in the name of your font : Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0)
最后,您在字体名称中犯了一个错误: Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0)
That's it! 而已!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.