代码之家  ›  专栏  ›  技术社区  ›  Domness

可调整大小的UILabel,可在屏幕上滚动

  •  10
  • Domness  · 技术社区  · 17 年前

    输入到标签中的文本由UITextField和UIButton完成。这个更新很好。

    但是我试图让UILabel根据输入的文本量调整大小,这样整个文本就会在屏幕上滚动。

    这是我目前拥有的滚动标签代码:

    [lblMessage setText: txtEnter.text];
    
    CABasicAnimation *scrollText;
    
    scrollText=[CABasicAnimation animationWithKeyPath:@"position.x"];
    scrollText.duration = 3.0;
    scrollText.repeatCount = 10000;
    scrollText.autoreverses = NO;
    scrollText.fromValue = [NSNumber numberWithFloat:500];
    scrollText.toValue = [NSNumber numberWithFloat:-120.0];
    
    [[lblMessage layer] addAnimation:scrollText forKey:@"scrollTextKey"];
    

    问题是,有时在屏幕中间开始滚动,有时在完全消失之前消失。

    由于标签的大小相同,它还会剪切文本。。我不知道如何改变这个。

    多姆

    2 回复  |  直到 17 年前
        1
  •  7
  •   Community Mohan Dere    9 年前

    我相信类似于 the solution for this question 这对这个案子很有用。

    [lblMessage setText: txtEnter.text];
    [lblMessage sizeToFit];
    scrollView.contentSize = lblMessage.frame.size;
    

    然后是上述链接问题中描述的平移动画代码,要平移的帧位于UILabel的最右侧。这将导致文本在标签上以恒定速率滚动。如果希望标签回滚到开头,可以在动画块的开头使用UIView setAnimationRepeatAutoreverses:和setAnimationRepeatCount:方法。

        2
  •  2
  •   Domness    17 年前

    这个标签很有效,谢谢!

    现在,我把标签放到了scrollview中,我看到了它。。但我现在不确定要添加的确切动画。我在你给我的链接上尝试了一些,但它们不起作用。

    CGPointMake(x,x)是contentOffset所需要的。