UILabel像选框一样自动滚动 [英] UILabel auto scroll like a marquee

查看:50
本文介绍了UILabel像选框一样自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让UI标签像滚动字幕一样自动滚动其中的文本.以下代码的工作方式取决于我向其投放的文本/内容的类型.我无法分辨什么有效,什么无效.发生的情况是该标签什么都没有显示,然后突然以极高的速度滚动了文本,使您无法阅读.

I am trying I make a UI label auto scroll the text that is inside it like a marquee. The below code works depending on what kind of text/content I throw at it. I can't tell what works and what does not. What happens is that the label does not show anything, then suddenly scrolls the text at an extremely high speed that you can't read it.

由于某些原因,这在iPhone上完美运行,但在iPad上却无法正常运行.我猜是因为我在iPhone上输入的文字总是比Uilabel的文字大很多?

For some reason, this works perfectly on the iPhone but not on the iPad. I am guessing because the text I am putting at the iPhone is always way bigger than the Uilabel's size?

这是代码:

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];

}

推荐答案

代码如下:

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

AutoScrollLabel, extended from UIView (code provided below) has an instance method to set text (setLabelText). The text provided will auto scroll only if the length of the text is bigger than the width of the AutoScrollLabel view.

例如:

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

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

Note: You can make changes in setLabelText: Method implementation to support Attributed Text (use appropriate methods to find size of Attributed Text)

*** AutoScrollLabel.h

*** 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

**** 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;
}

这篇关于UILabel像选框一样自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆