【问题标题】:Change UILabel based on UIButton control events根据 UIButton 控件事件更改 UILabel
【发布时间】:2012-08-01 23:09:39
【问题描述】:

我有一个UIButton,它有一个默认图像,以及另一个用于突出显示/选定图像的图像。按下按钮时,按钮的图像变为突出显示,如果 touchUp 在内部,则变为选中。通常的东西.. IB 中的所有设置。

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

我尝试在 IB 中的按钮上添加标签。问题:我需要标签的文本颜色随着按钮控件状态的变化而变化。

所以,我创建了一个UIButton 子类,添加了一个UILabel Outlet,并通过覆盖以下方法:

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

我能够实现我想要的……但是!当我快速单击按钮时,不会反映更改。有时它不能正常工作......我什至使用了异步调用......没用。

所以,我前往UIButtontitleLabel。我尝试使用它,但没有运气。

所以,我尝试了UIButton setTitle: forState:,没有用...帮助?


额外细节:

- (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; 如果(自我){ [self.titleLabel setFrame:CGRectMake(0, 0, 100, 100)]; [self.titleLabel setText:@"标题标签"]; [self.titleLabel setHidden:NO]; [self.imageView setAlpha:0.2f]; NSLog(@"%@", self.subviews); [自我设置标题:@“默认!!” forState:UIControlStateNormal]; } 回归自我; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [超级 touchesBegan:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [超级 touchesMoved:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [超级 touchesCancelled:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [超级 touchesEnded:touches withEvent:event]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } - (void)setSelected:(BOOL)selected { [超级setSelected:selected]; [self performSelector:@selector(check) withObject:nil afterDelay:0.1]; } -(无效)检查{ if (self.isSelected || self.state == UIControlStateHighlighted || self.state == UIControlStateSelected) { [_label setHighlighted:YES]; } 别的 { [_label setHighlighted:NO]; } }

输出:

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

【问题讨论】:

  • :/ .. 最后,我为按钮上方的UILabel 添加了背景颜色,使其与按钮的默认状态相匹配。突出显示后,标签出现边框...

标签: ios uikit uibutton uilabel uicontrolevents


【解决方案1】:

您需要为 UIButton 设置背景图像...可能是您正在设置按钮的图像属性...这就是为什么您的标签与图像重叠...。

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

 [optionButtonOutlet setBackgroundImage:[UIImage imageNamed:@"predict_match_btn_blue_pressed@2x.png" ]forState:UIControlStateNormal];

【讨论】:

  • 感谢您的回答。我的标签看起来很完美,没有重叠。我只需要它根据控件状态更改颜色将其位置更改为右上角(例如)。
【解决方案2】:

经过这么几个月,我找到的最佳答案是覆盖:

- (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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多