1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSTimer to scroll text into a textField?

Discussion in 'Mac Programming' started by HungrySeacow, Mar 3, 2006.

  1. macrumors regular

    #1
    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? :)
     
  2. macrumors 68030

    Catfish_Man

    #2
    Just a quick off-the-top-of-my head to give you ideas...

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

    #3
    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 :).
     
  4. Guest

    caveman_uk

    #4
    You may be interested in this link on scrolling views Link.
    And also the example here
     
  5. macrumors regular

    #5
    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.
     
  6. macrumors regular

    #6
    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.
     

Share This Page