Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HungrySeacow

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Just a quick off-the-top-of-my head to give you ideas...

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

HungrySeacow

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
Catfish_Man said:
Just a quick off-the-top-of-my head to give you ideas...

Code:
- (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 :).
 

HungrySeacow

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
HungrySeacow said:
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:
Code:
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

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
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:
Code:
#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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.