首页 文章

根据UIButton控件事件更改UILabel

提问于
浏览
1

我有一个 UIButton ,它有一个默认图像,另一个图像用于高亮/选定图像 . 按下按钮时,按钮的图像会变为高亮显示,然后在触摸按钮位于内部时变为选定状态 . 通常的东西..在IB内的所有设置 .

但是,我试图在一个非常棘手的地方(未对齐,硬编码)在按钮上设置标签 .

我尝试在IB的按钮上添加一个Label . 问题:当按钮的控件状态发生变化时,我需要更改标签的文本颜色 .

所以,我创建了一个 UIButton 子类,添加了一个 UILabel Outlet,并重写了以下方法:

- (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>>"
)

2 回答

  • 0

    你需要为UIButton设置背景图像...可能你正在设置按钮的图像属性...这就是你的标签被图像重叠的原因....

    [optionButtonOutlet  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
     [optionButtonOutlet setBackgroundImage:[UIImage imageNamed:@"predict_match_btn_blue_pressed@2x.png" ]forState:UIControlStateNormal];
    
  • 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;
    }
    

相关问题