2012-08-03 61 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