您的位置:首页 > 产品设计 > UI/UE

UILabel文字滚动

2015-07-23 11:26 519 查看
-(void)viewDidLoad

{

timer = [[NSTimer alloc] init];



mainsco = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

mainsco.backgroundColor = [UIColor clearColor];

[bgView addSubview:mainsco];

//显示广告内容

noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

noticeLabel.numberOfLines = 0;

noticeLabel.font = [UIFont systemFontOfSize:13.0f];

noticeLabel.backgroundColor = [UIColor clearColor];

[mainsco addSubview:noticeLabel];



noticeLabel.text =[dic valueForKey:@"Bulletin"];



noticeSize = [ [dic valueForKey:@"Bulletin"] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(180, 20000000)];

//所有文字显示在一行所占的高度

size1 = [[dic valueForKey:@"Bulletin"] sizeWithFont:[UIFont systemFontOfSize:13.0f]];

mainsco.contentSize = CGSizeMake(180, noticeSize.height);

mainsco.showsVerticalScrollIndicator = NO;

//根据文字的多少设置label的宽高,但底层的scrollview只显示一行内容的高度

noticeLabel.frame = CGRectMake(0, 0, 180, noticeSize.height);



mainsco.frame =CGRectMake(116, 77, 180, size1.height);

if (noticeSize.height>size1.height)

{

//如果文字大于一行就开始滚动,否则停止timer

timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(time) userInfo:self repeats:YES];



}else{

[timer invalidate];

}



//滚动的方法

-(void)time

{

//oldy用来记录上一次scrollview滚动的位置

mainsco.contentOffset = CGPointMake(0, oldy);

if (oldy>noticeSize.height-1) {

oldy = 0;

}else

oldy++;//设置每次滚动的高度,即几个像素

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: