2012-08-03 118 views
1

我有一個UIButton,它有一個默認圖像,另一個圖像用於高亮/選定圖像。按下按鈕時,按鈕的圖像將變爲高亮顯示,如果touchUp在裏面,則顯示選定的圖像。通常的東西..所有在IB內設置。根據UIButton控件事件更改UILabel

但是,我試圖在一個非常棘手的地方(不對齊,硬編碼)在按鈕上設置標籤。

我試着在IB的按鈕上添加一個標籤。問題:我需要標籤的文字顏色隨着按鈕的控制狀態改變而改變。

所以,我創建了一個UIButton子類,增加了一個UILabel出口,並通過重寫以下方法:

- (void)touchesBegan/Moved/Cancelled/Ended:...; 
- (void)setSelected:... 

我能達到我想要的......但是!當我快速點擊按鈕時,更改不會反映出來。有時它不能正常工作......我甚至使用異步調用......沒用。所以我去UIButtontitleLabel。我試圖用它沒有運氣。

所以,我試過UIButton setTitle: forState:,沒用...幫助?


額外的細節:

 
- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self.titleLabel setFrame:CGRectMake(0, 0, 100, 100)]; 
     [self.titleLabel setText:@"THE TITLE LABEL"]; 
     [self.titleLabel setHidden:NO]; 

     [self.imageView setAlpha:0.2f]; 
     NSLog(@"%@", self.subviews); 


     [self setTitle:@"DEFAULT!!" forState:UIControlStateNormal]; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; 

} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesCancelled:touches withEvent:event]; 
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; 

} 

- (void)setSelected:(BOOL)selected { 
    [super setSelected:selected]; 
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; 
} 

- (void)check { 
    if (self.isSelected || self.state == UIControlStateHighlighted || self.state == UIControlStateSelected) { 
     [_label setHighlighted:YES]; 
    } else { 
     [_label setHighlighted:NO]; 
    } 
} 

OUTPUT:

(
    "<UIImageView: 0x8b24930; frame = (0 0; 243 39); clipsToBounds = YES; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b248e0>>", 
    "<UIButtonLabel: 0x8b247a0; frame = (0 0; 100 100); text = 'THE TITLE LABEL'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b25000>>" 
) 
+0

:/ ..最後,我添加了一個背景顏色到'UILabel'這是在按鈕,使其與按鈕的默認狀態相匹配。 Upoen突出顯示,標籤顯示有邊... – Mazyod 2012-08-04 01:47:09

回答

0

最好的答案,我發現,所有這些個月後,是重寫:

- (CGRect)titleRectForContentRect:(CGRect)contentRect; 

要我想要的方式定位的標籤。這使我能夠利用的:

[self setTitleColor:[UIColor offWhiteColor] forState:UIControlStateNormal]; 
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateSelected]; 
[self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateHighlighted]; 

對於更加好奇:

- (CGRect)titleRectForContentRect:(CGRect)contentRect { 
    CGSize margin = CGSizeMake(15.f, 5.f); 
    CGRect clay = contentRect; 
    clay = CGRectInset(clay, margin.width, 0.f); 
    clay.origin.x += margin.width; 
    clay.origin.y += margin.height; 

    return clay; 
} 
0

您需要設置UIButton的背景圖片...可能是要設置按鈕的圖像propertry ...爲什麼你的標籤被圖像重疊......

[optionButtonOutlet setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 

[optionButtonOutlet setBackgroundImage:[UIImage imageNamed:@"[email protected]" ]forState:UIControlStateNormal]; 
+0

感謝您的回答。我的標籤看起來很完美,沒有重疊。我只需要它根據控制狀態**改變顏色,並將它的位置改爲右上角(例如)。 – Mazyod 2012-10-12 11:33:32