@interface RollingTextBar : UIView
{
UILabel* _label;
NSTimer* _timer;
float _w_text;
float _w_show;
UIFont* _font;
}
-(id) initWithText:(NSString*)text withFrame:(CGRect)rt withFont:(UIFont*)font withSpd:(float)spd;
@end
#import "RollingTextBar.h"
@implementation RollingTextBar
-(id) initWithText:(NSString*)text withFrame:(CGRect)rt withFont:(UIFont*)font withSpd:(float)spd
{
if(!(self = [super initWithFrame:rt]))
{
return nil;
}
self.clipsToBounds = YES;
self.userInteractionEnabled = NO;
_w_show = rt.size.width;
float h = rt.size.height;
CGSize textSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, font.lineHeight)];
NSLog(@"fontheight : %f",font.lineHeight);
_w_text = textSize.width;
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)];
_label.center = CGPointMake(_w_show + _w_text/2, h/2);
[_label setText:text];
[_label setTextColor:[UIColor blackColor]];
//[_label setBackgroundColor:[UIColor yellowColor]];
[_label setBackgroundColor:[UIColor clearColor]];
[_label setTextAlignment:UITextAlignmentLeft];
[_label setFont:font];
[_label setOpaque:YES];
[self addSubview:_label];
[_label release];
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0/spd
target:self
selector:@selector(stepLabel)
userInfo:nil
repeats:YES];
return self;
}
-(void) stepLabel
{
_label.center = CGPointMake(_label.center.x - 2, _label.center.y);
if(_label.center.x < (-_w_text/2))
{
_label.center = CGPointMake(_w_show + _w_text/2, _label.center.y);
}
NSLog(@"step label");
}
-(void) removeFromSuperview
{
if([_timer isValid])
{
[_timer invalidate];
}
[super removeFromSuperview];
}
@end