Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tap label to toggle between two pieces of text. A nice trick to de-clutter an interface.
//
// DoubleLabel.h
// LongTermTimer
//
// Created by Jason Moore on 11-05-31.
// Copyright 2011 xinsight. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DoubleLabel : UIView {
UILabel *front;
UILabel *back;
UILabel *end;
BOOL isFrontVisible;
}
@property (nonatomic,assign) NSString* frontText;
@property (nonatomic,assign) NSString* backText;
- (id) initWithFrame:(CGRect)frame Front:(NSString*)front_ Back:(NSString*)back_;
@end
//
// DoubleLabel.m
// LongTermTimer
//
// Created by Jason Moore on 11-05-31.
// Copyright 2011 xinsight. All rights reserved.
//
#import "DoubleLabel.h"
@implementation DoubleLabel
@synthesize frontText, backText;
- (void) dealloc;
{
[front release];
[back release];
[end release];
[super dealloc];
}
- (id) initWithFrame:(CGRect)frame;
{
return [self initWithFrame:frame Front:@"" Back:@""];
}
- (id) initWithFrame:(CGRect)frame Front:(NSString*)front_ Back:(NSString*)back_;
{
self = [super initWithFrame:frame];
if (self) {
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
label.backgroundColor = [UIColor clearColor];
label.textColor = UIColorFromRGBHex(0x848484);
label.textAlignment = UITextAlignmentCenter;
label.text = front_;
label.font = [UIFont systemFontOfSize:14];
front = label;
[self addSubview:front];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
label.backgroundColor = [UIColor clearColor];
label.textColor = UIColorFromRGBHex(0x848484);
label.textAlignment = UITextAlignmentCenter;
label.text = back_;
label.font = [UIFont systemFontOfSize:14];
label.alpha = 0;
back = label;
[self addSubview:back];
label = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width-10-10, 0, 10, frame.size.height)];
label.backgroundColor = [UIColor clearColor];
label.textColor = UIColorFromRGBHex(0xeeeeee);
label.textAlignment = UITextAlignmentCenter;
label.text = @"";
label.font = [UIFont systemFontOfSize:21];
label.alpha = 1.0;
end = label;
[self addSubview:end];
isFrontVisible = YES;
}
return self;
}
- (void) setFrontText:(NSString*)front_;
{
front.text = front_;
}
- (void) setBackText:(NSString*)back_;
{
back.text = back_;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
[UIView animateWithDuration:0.3 animations:^{
isFrontVisible = !isFrontVisible;
if (isFrontVisible) {
back.alpha = 0.0;
end.frame = CGRectMake(self.frame.size.width-10-10, 0, 10, self.frame.size.height);
} else {
front.alpha = 0.0;
end.frame = CGRectMake(10, 0, 10, self.frame.size.height);
}
} completion:^(BOOL finished){
[UIView animateWithDuration:0.3 animations:^{
if (isFrontVisible) {
front.alpha = 1.0;
} else {
back.alpha = 1.0;
}
}];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.