Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

objective c - How to make marquee UILabel / UITextField / NSTextField

I need to make marquee UILabel in Xcode. The marquee will scroll from right to left.

I tried CCScrollingLabel also JHTickerView and others. But i can not find simple code with marquee with out any views/arrays/some stupid libraries and others

How to make marquee with UILabel? (UITextField and NSTextField are OK too).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need NSTimer and you need to invoke a method something as :

*This is for OSX, you can easily convert it into iOS, NSTextField and UILabel has different methods.

-(void)awakeFromNib{
    self.myTimer=[NSTimer new];
    self.fullString=@"This is a long string to be shown.";
    [self.label setAlignment:NSRightTextAlignment];
}


-(void)scrollText:(id)parameter{
    static NSInteger len;
    [self.label setStringValue:[self.fullString substringWithRange:NSMakeRange(0, len++)]];

    if (self.label.stringValue.length==self.fullString.length) {
        [self.myTimer invalidate];
    }
}

- (IBAction)buttonAction:(id)sender {

    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2f
                                                    target: self
                                                  selector:@selector(scrollText:)
                                                  userInfo: nil 
                                                   repeats:YES];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...