UILabel文字滚动

来源:互联网 发布:双代号网络计划图 编辑:程序博客网 时间:2024/05/16 18:49

-(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++;//设置每次滚动的高度,即几个像素
}
0 0
原创粉丝点击