Skip to content

Instantly share code, notes, and snippets.

@xinsight
Created March 2, 2012 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xinsight/1956700 to your computer and use it in GitHub Desktop.
Save xinsight/1956700 to your computer and use it in GitHub Desktop.
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