UILabel文字滚动


-(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++;//设置每次滚动的高度,即几个像素
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值