我试图突出我的UIButton与自定义UIColor浅灰色,并使文本变白…但我不知道如何做到这…这是我创建UIButton到目前为止任何帮助将不胜感激.
cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; cancelButton.frame = CGRectMake(10.0,calculatorView.frame.size.height-55,100.0,45.0); [cancelButton addTarget:self action:@selector(calculateMethod:)forControlEvents:UIControlEventTouchDown]; [[cancelButton titleLabel] setFont:[UIFont fontWithName: @"Super-text" size:20.0]]; [cancelButton setTitle:@"Cancel" forState:UIControlStatenormal];
解决方法
要获得突出显示的白色文本,请执
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
要获得更改背景,您需要执行setBackgroundImage:forState:并使用带图案颜色的UIImage或子类UIButton,并在setHighlight:方法中设置适当的背景颜色.
编辑:Swift 2.x版本
cancelButton.setTitleColor(.whiteColor(),forState: Highlighted)
编辑:Swift 3.0版本
cancelButton.setTitleColor(.white,for: .highlighted)