這是一個非常奇怪的過程。UIButton的標題顏色在突出顯示/選中時不會改變,但背景顏色將會變爲
我有一個IBOutletCollection的UIButtons。通過收集我環路和創建它們像這樣(的displayHourButtons
從viewWillAppear
叫):
- (void)displayHourButtons
{
// Counter
NSUInteger b = 0;
// Set attributes
UIFont *btnFont = [UIFont fontWithName:@"Metric-Semibold" size:13.0];
UIColor *btnTextColor = [UIColor colorWithRed:(147/255.0f) green:(147/255.0f) blue:(147/255.0f) alpha:1.0];
NSNumber *btnTracking = [NSNumber numberWithFloat:0.25];
NSMutableParagraphStyle *btnStyle = [[NSMutableParagraphStyle alloc] init];
[btnStyle setLineSpacing:2.0];
NSDictionary *btnAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
btnFont, NSFontAttributeName,
btnTextColor, NSForegroundColorAttributeName,
btnTracking, NSKernAttributeName, nil];
// CREATE THE BUTTONS
for (UIButton *hourButton in hourButtons) {
// I'm using the attributed string for something else
// later in development that I haven't got to yet.
// I simplified the string for this example's sake.
NSString *btnTitleText = [NSString stringWithFormat:@"Button %lu", (unsigned long)b];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:btnTitleText
attributes:btnAttrs];
[attributedText addAttribute:NSParagraphStyleAttributeName
value:btnStyle
range:NSMakeRange(0, btnTitleText.length)];
CALayer *btnLayer = [hourButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:19.0f];
[hourButton setTag:b];
[hourButton setContentEdgeInsets:UIEdgeInsetsMake(5.0, 1.0, 0.0, 0.0)];
[hourButton setAttributedTitle:attributedText forState:UIControlStateNormal];
[hourButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[hourButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
hourButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[hourButton addTarget:self action:@selector(showHour:) forControlEvents:UIControlEventTouchUpInside];
b++;
}
}
當點擊一個按鈕,每個動作showHour:
被稱爲:
- (IBAction)showHour:(id)sender
{
[self.hourButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIButton *button = (UIButton *)obj;
if (button != sender && button.enabled) {
// This is applied. I know because I tested it with redColor
[button setBackgroundColor:[UIColor clearColor]];
// Doesn't change, stays the gray set initially
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
else {
// This is applied
[button setBackgroundColor:[UIColor colorWithRed:(169/255.0f) green:(234/255.0f) blue:(255/255.0f) alpha:1.0]];
// This is not
[button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted)];
}
}];
// displayHour uses the tag to change labels, images, etc.
[self displayHour:(long int)[sender tag]];
}
我試着各種瘋狂的事情讓UIImage處於選定狀態,但沒有任何工作。這枚枚舉對象是唯一有效的工作。這就是爲什麼我說這是一個奇怪的過程。我猜按鈕不會無限期地保持活動狀態?
反正,我的問題:爲什麼標題顏色沒有改變是否有某種原因?只是背景?我懷疑它與最初沒有設置的背景有關,但我無法解釋爲什麼。
謝謝!
修訂
每@Timothy駝鹿的回答,下面是更新的代碼。
- (IBAction)showHour:(id)sender
{
[self.hourButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIButton *button = (UIButton *)obj;
// Grab the mutable string from the button and make a mutable copy
NSMutableAttributedString *attributedText = [[button attributedTitleForState:UIControlStateNormal] mutableCopy];
// Shared attribute styles
UIFont *btnFont = [UIFont fontWithName:@"Metric-Semibold" size:14.0];
NSNumber *btnTracking = [NSNumber numberWithFloat:0.25];
NSMutableParagraphStyle *btnStyle = [[NSMutableParagraphStyle alloc] init];
[btnStyle setLineSpacing:2.0];
// Since we can't set a color directly on a Attributed string we have
// to make a new attributed string.
if (button != sender && button.enabled) {
// Return to the default color
UIColor *btnTextColor = [UIColor colorWithRed:(147/255.0f) green:(147/255.0f) blue:(147/255.0f) alpha:1.0];
// Set up attributes
NSDictionary *btnAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
btnFont, NSFontAttributeName,
btnTextColor, NSForegroundColorAttributeName,
btnTracking, NSKernAttributeName, nil];
// Reapply the default color (for the one button that was changed to white)
[attributedText setAttributes:btnAttrs
range:NSMakeRange(0, attributedText.length)];
// Add line-height
[attributedText addAttribute:NSParagraphStyleAttributeName
value:btnStyle
range:NSMakeRange(0, attributedText.length)];
// Reset default attributes
[button setBackgroundColor:[UIColor clearColor]];
[button setAttributedTitle:attributedText forState:UIControlStateNormal];
}
else {
// Our new white color for the active button
UIColor *btnTextColor = [UIColor whiteColor];
// Set up attributes
NSDictionary *btnAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
btnFont, NSFontAttributeName,
btnTextColor, NSForegroundColorAttributeName,
btnTracking, NSKernAttributeName, nil];
// Apply our new white color
[attributedText setAttributes:btnAttrs
range:NSMakeRange(0, attributedText.length)];
// Add line-height
[attributedText addAttribute:NSParagraphStyleAttributeName
value:btnStyle
range:NSMakeRange(0, attributedText.length)];
// Add new attributes for active button
[button setBackgroundColor:[UIColor colorWithRed:(169/255.0f) green:(234/255.0f) blue:(255/255.0f) alpha:1.0]];
[button setAttributedTitle:attributedText forState:UIControlStateNormal];
}
}];
[self displayHour:(long int)[sender tag]];
}
是的,就是這樣。謝謝! –