无法在 swift 中的循环内更改 textColor 和字体大小
Cannot change textColor and font size inside the loop in swift
我不知道为什么字体大小和文本颜色在循环中没有任何效果...有人帮忙吗??
这是我的代码:
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
您应该直接编辑按钮,而不是 titleLabel
,像这样:
Button.setTitle("\(button)", forState: .Normal)
Button.setTitleColor(UIColor.blackColor(), forState: .Normal)
首先删除行Button.titleLabel?.text = "\(button)"
。
这个就够了Button.setTitle("\(button)", forState: UIControlState.Normal)
.
那么你应该使用按钮中的 setTitleColor
方法来改变它。
Button.setTitleColor(UIColor. blackColor(), forState: UIControlState.Normal)
最后你的字体名称弄错了:
Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0)
就是这样!
我不知道为什么字体大小和文本颜色在循环中没有任何效果...有人帮忙吗?? 这是我的代码:
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
您应该直接编辑按钮,而不是 titleLabel
,像这样:
Button.setTitle("\(button)", forState: .Normal)
Button.setTitleColor(UIColor.blackColor(), forState: .Normal)
首先删除行Button.titleLabel?.text = "\(button)"
。
这个就够了Button.setTitle("\(button)", forState: UIControlState.Normal)
.
那么你应该使用按钮中的 setTitleColor
方法来改变它。
Button.setTitleColor(UIColor. blackColor(), forState: UIControlState.Normal)
最后你的字体名称弄错了:
Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0)
就是这样!