UILabel自动滚动就像一个选框

时间:2014-10-04 16:20:43

标签: ios uilabel

我正在尝试将UI标签自动滚动到其中的文本,就像一个选框。下面的代码取决于我抛出的文本/内容类型。我无法分辨哪些有用,哪些无效。发生的事情是标签没有显示任何内容,然后突然以非常高的速度滚动文本,你无法读取它。

出于某种原因,这在iPhone上完美运行,但在iPad上则不然。我猜是因为我在iPhone上放的文字总是大于Uilabel的尺寸?

以下是代码:

self.currentTrack.textColor = [UIColor colorWithRed:15.0/255.0 green:176.0/255.0 blue:223.0/255.0 alpha:1];
self.currentTrack.labelSpacing = 35; // distance between start and end labels
self.currentTrack.pauseInterval = 1.7; // seconds of pause before scrolling starts again
self.currentTrack.scrollSpeed = 30; // pixels per second
self.currentTrack.textAlignment = NSTextAlignmentCenter; // centers text when no auto-scrolling is applied
self.currentTrack.fadeLength = 12.f;
self.currentTrack.scrollDirection = CBAutoScrollDirectionLeft;
[self.currentTrack observeApplicationNotifications];

}

2 个答案:

答案 0 :(得分:3)

以下是代码:

AutoScrollLabel(下面提供的代码)扩展的

UIView有一个实例方法来设置文本(setLabelText)。仅当文本的长度大于AutoScrollLabel视图的宽度时,提供的文本才会自动滚动。

例如:

AutoScrollLabel *autoScrollLabel = [[AutoScrollLabel alloc] initWithFrame:CGRectMake(0, 0, 150, 25)];
[autoScrollLabel setLabelText:@"Some lengthy text to be scrolled"];

注意:您可以在setLabelText:方法实现中进行更改以支持属性文本(使用适当的方法查找属性文本的大小)

*** AutoScrollLabel.h

@interface AutoScrollLabel : UIView {
    UILabel *textLabel;
    NSTimer *timer;
}

- (void) setLabelText:(NSString*) text;
- (void) setLabelFont:(UIFont*)font;
- (void) setLabelTextColor:(UIColor*)color;
- (void) setLabelTextAlignment:(UITextAlignment)alignment;
@end

**** AutoScrollLabel.m

#import "AutoScrollLabel.h"
#import <QuartzCore/QuartzCore.h>

@implementation AutoScrollLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       // Initialization code
        self.clipsToBounds = YES;
        self.autoresizesSubviews = YES;

        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,   frame.size.height)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor blackColor];
        textLabel.textAlignment = UITextAlignmentRight;
        [self addSubview:textLabel];
    }
    return self;
}

-(void) setLabelText:(NSString*) text {

    textLabel.text  = text;
    CGSize textSize = [text sizeWithFont:textLabel.font];

    if(textSize.width > self.frame.size.width) {
        textLabel.frame = CGRectMake(0, 0, textSize.width, self.frame.size.height);
    }
    else {
        textLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }

    if(textLabel.frame.size.width > self.frame.size.width) {

      [timer invalidate];
      timer = nil;
      CGRect frame = textLabel.frame;
      frame.origin.x = self.frame.size.width-50;
      textLabel.frame = frame;
      timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveText) userInfo:nil repeats:YES];
    }
    else {
        [timer invalidate];
        timer = nil;
    }
  }

-(void) moveText {

    if(textLabel.frame.origin.x < textLabel.frame.size.width-2*textLabel.frame.size.width) {
        CGRect frame = textLabel.frame;
        frame.origin.x = self.frame.size.width;
        textLabel.frame = frame;
    }
    [UIView beginAnimations:nil context:nil];
    CGRect frame = textLabel.frame;
    frame.origin.x -= 5;
    textLabel.frame = frame;
    [UIView commitAnimations];

}

- (void) setLabelFont:(UIFont*)font {
    textLabel.font = font;
}

- (void) setLabelTextColor:(UIColor*)color {
    textLabel.textColor = color;
}

- (void) setLabelTextAlignment:(UITextAlignment)alignment {
    textLabel.textAlignment = alignment;
}

答案 1 :(得分:0)

对于 Swift5

将UILabel的顶部设置为与superview的底部相等,然后使用以下代码:

 func createAnimationLbl() -> CAAnimation {
        let animation = CABasicAnimation(keyPath: "transform.translation.y")
        animation.fromValue = 0
        animation.toValue = -(self.view.frame.height )
        animation.duration = 15
        animation.repeatCount = Float.infinity //times of scrolling, infinity for unlimited times.
        
        animation.timingFunction =  CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.linear)
        animation.fillMode = CAMediaTimingFillMode.forwards
        animation.isRemovedOnCompletion = false
        return animation
        
    }

然后在您的viewWillAppear中将其命名为:

self.aboutLbl.layer.add(createAnimationLbl(), forKey: nil)