继承uiview的滚动字符view

@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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值