PDA

View Full Version : NSTimer to scroll text into a textField?




HungrySeacow
Mar 3, 2006, 07:26 PM
I have been trying to figure out how to get text to scroll into a textField. I thought that I might be able to do with an NSTimer that calls a method which would increase an NSString one character at a time and then set the value of the textField to the string before the NSTimer would fire again. However, I feel clueless with this. Anyone think they could help me out with this? :)



Catfish_Man
Mar 3, 2006, 08:30 PM
Just a quick off-the-top-of-my head to give you ideas...


- (void) methodCalledByTimer
{
[[textView textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"a"] autorelease]];
}

HungrySeacow
Mar 3, 2006, 09:00 PM
Just a quick off-the-top-of-my head to give you ideas...


- (void) methodCalledByTimer
{
[[textView textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"a"] autorelease]];
}


Thanks for the idea.
I was wondering why you choose an NSAttribuitedString rather then an NSString?

I tested out the code under my IBAction method for the button that activates the sequence bypassing the NSTimer to see if I could get it to work but I get an error when the method is called, "-[NSTextField textStorage]: selector not recognized [self = 0x31c4c0]". I so tried a setup using an NSString instead but got a similar selector error. I just finished reading a few cocoa books and am still having trouble figuring out how to create code myself. At least I now know what polymorphism means, LOL. Any help is greatly appreciated :).

caveman_uk
Mar 4, 2006, 03:22 AM
You may be interested in this link on scrolling views Link (http://developer.apple.com/documentation/Cocoa/Conceptual/DrawViews/Tasks/ScrollingInCode.html).
And also the example here (http://cocoadevcentral.com/articles/000044.php)

HungrySeacow
Mar 4, 2006, 12:26 PM
Thanks for the idea.
I was wondering why you choose an NSAttribuitedString rather then an NSString?

I tested out the code under my IBAction method for the button that activates the sequence bypassing the NSTimer to see if I could get it to work but I get an error when the method is called, "-[NSTextField textStorage]: selector not recognized [self = 0x31c4c0]". I so tried a setup using an NSString instead but got a similar selector error. I just finished reading a few cocoa books and am still having trouble figuring out how to create code myself. At least I now know what polymorphism means, LOL. Any help is greatly appreciated :).

Ahh, I am a dumba**, I looked at the documentation and see that textStorage is for textViews and not textFields. I now have this and it is working:
mutableString = [[NSMutableString alloc] initWithString:@"string to apend to."];
[mutableString appendString:[[[NSMutableString alloc] initWithString:@"a"] autorelease]];
[textField1 setStringValue: mutableString];
Thanks again for helping! Now I will try and figure out how to get this in a loop or so to do what I am looking for.

HungrySeacow
Mar 4, 2006, 01:12 PM
Yay! I got what I wanted to work! Thanks again Catfish Man for leading me down the right path! Here is the code that I ended up with:
#import "AppController.h"

@implementation AppController


- (IBAction)start:(id)sender
{
stringData = [[NSString alloc] initWithString:@"String to be scrolling :)"];
mutableString = [[NSMutableString alloc] initWithString:@""];
x = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(changeText)
userInfo:nil
repeats:YES];
}

- (void)changeText
{
theChar = [stringData characterAtIndex:x];
intermediateMutableString = [NSMutableString stringWithCharacters:&theChar length:1];
[mutableString appendString:intermediateMutableString];
[textField1 setStringValue: mutableString];
x++;
if ([mutableString length] == [stringData length]) {
[timer invalidate];
}
}

@end

EDIT: Oh I forgot to ask what this warning was about that I get from the compiler from this line: "intermediateMutableString = [NSMutableString stringWithCharacters:&theChar length:1];"
The warning is: "warning: passing argument 1 of 'stringWithCharacters:length:' from incompatible pointer type". The program works as I wanted at least.