bneupaane
bneupaane

Reputation: 537

Animate text from right to left in UITextField

I have a long text that I need to display in one line. Text is too long to fit in the UITextField object. I'm looking for a way to animate the text (or the uitextfield object itself) from right to left. I have seen that in some iPhone app but don't know how to do it.

The idea is, if the text is big to fit, scroll it slowly to left so that the user can read it.

Any idea how this can be achieved ?

Thank you!

Upvotes: 2

Views: 3896

Answers (4)

adamup
adamup

Reputation: 1538

There's now a drop-in replacement for UILabel that will take care of this:

https://github.com/cbpowell/MarqueeLabel

Overview: MarqueeLabel is a UILabel subclass adds a scrolling marquee effect when the text of the label outgrows the available width. The label scrolling direction and speed/rate can be specified as well. All standard UILabel properties (where it makes sense) are available in MarqueeLabel and it behaves just like a UILabel.

I am not an author of MarqueeLabel or related to the authors in any way.

Upvotes: 1

Yogesh Maheshwari
Yogesh Maheshwari

Reputation: 1323

There is one solution but not really my fav.. If you have AutoScrollLabel *marqueeLabel in your viewController then set its text in viewWillAppear method of viewController as:-

marqueeLabel.text = marqueeLabel.text;

I haven't found anything other than this. Also if you are having that problem then try using multitasking in your iphone/ipod and when you will open your application again.. you will not find marqueeLabel animating.. for that add a statement in commonInit of AutoScrollLabel class:-

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readjustLabels) name:UIApplicationWillEnterForegroundNotification object:nil];

and also add one more statement in dealloc of that class

[[NSNotificationCenter defaultCenter] removeObserver:self];

Upvotes: 0

owenfi
owenfi

Reputation: 2641

You could use a solution like Brian Stormont's AutoScrollLabel with more discussion here.

After including that you need to do roughly the following:

AutoScrollLabel *marquee = [[AutoScrollLabel alloc] initWithFrame:marqueeFrame];
[view addSubview:marquee];
[marquee setScrollSpeed:16.8];
marquee.text = @"My long text";
[marquee readjustLabels];

[marquee scroll]; //This line is the one that actually starts the scrolling

It is pretty old, but I'm using it in a shipping app now.

Tangentially related, but not required, is this animated text class AUIAnimatedText which allows you to animate other properties of text like size and color.

Upvotes: 2

Martin Pilch
Martin Pilch

Reputation: 3305

I have not done anything like this, but I think you can do this overriding textRectForBounds: method (UITextField documentation) and using NSTimer. In the timer you should just call setNeedsDisplay method of the text field.

Upvotes: 0

Related Questions