Elfoiros
Elfoiros

Reputation: 468

How to change the text Color within the drawRect of a custom NSTextfield

I use a own class OMNTextfield subclass of NSTextfield to display text and animate the text if the text lenght is greater than the control width.

The text is animated like song title in iTunes. It work like a charm !

Now i want to change the text's color and the text alignment. I've search for a while setTextcolor, setAlignment, viewWillDraw, cellClass, ... but nothing work => My text is still black !

You can find the complete code here (Xcode project)

My question : How to change the text color/alignment in a NSTextfield subclass ?

Acoording to this post the code bellow should work but it doesn't !

-(void)viewWillDraw { // or whatever is the Appkit equivalent
      [super setTextColor:[NSColor redColor]];
}

Complete code :

//
//  OMNTextField.h


#import <Cocoa/Cocoa.h>

@interface OMNTextField : NSTextField {
    NSTimer * scroller;
    NSPoint point;
    NSString * text;
    NSTimeInterval speed;
    CGFloat stringWidth;
}

- (void) setText:(NSString *)newText;

@property (nonatomic, copy) NSString * text;
@property (nonatomic) NSTimeInterval speed;

@end

//
//  OMNTextField.m

#import "OMNTextField.h"

@implementation OMNTextField

@synthesize text;
@synthesize speed;


- (void) dealloc {
    [text release];
    [scroller invalidate];
    [super dealloc];
}

- (void) setText:(NSString *)newText {
    NSLog(@"[%@ %@] *** ", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    [text release];
    text = [newText copy];
    point = NSZeroPoint;

    stringWidth = [newText sizeWithAttributes:nil].width;

    if (scroller == nil && speed > 0 && text != nil) {
        scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
    }
}

- (void) setSpeed:(NSTimeInterval)newSpeed {
    if (newSpeed != speed) {
        speed = newSpeed;

        [scroller invalidate];
        if (speed > 0 && text != nil) {
            scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
        }
    }
}

-(void)viewWillDraw {
    [super setTextColor:[NSColor yellowColor]];
}


- (void) moveText:(NSTimer *)timer {
    if (stringWidth >= self.frame.size.width)
        point.x = point.x - 1.0f;

    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect {
    CGFloat max;

    if (stringWidth >= dirtyRect.size.width)
        max = stringWidth;
    else
        max = dirtyRect.size.width;

    if (point.x + stringWidth < 0) {
        point.x += max ;
    }

    [text drawAtPoint:point withAttributes:nil];

    if (point.x < 0) {
        NSPoint otherPoint = point;
        otherPoint.x += max;
        [text drawAtPoint:otherPoint withAttributes:nil];
    }

}

@end
//
//  AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "OMNTextField.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
}


@property (assign) IBOutlet OMNTextField *label2;
@property (assign) IBOutlet OMNTextField *label3;
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSView *view;

@end

//
//  AppDelegate.m


#import "AppDelegate.h"

@implementation AppDelegate

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    [self.label2 setText:@"Too shoort no need to move"];
    [self.label2 setSpeed:0.05];


    [self.label3 setText:@"Text Lenght > TextField.width => Automatically animate text to show all"];
    [self.label3 setSpeed:0.05];
}

@end

PS: My code is based on Dave Delong code : iTunes Song Title Scrolling in Cocoa

Upvotes: 2

Views: 3249

Answers (2)

Elfoiros
Elfoiros

Reputation: 468

As said Nate Chandler use NSDictionary *attributes for text color.

Regarding the text alignment, i managed to center the text by calculated the good value for drawAtPoint:point

We have all the needed information stringWidth, and the control NSTextfield width to calculate the good value for variable point in drawAtPoint:point

Upvotes: 0

Nate Chandler
Nate Chandler

Reputation: 4553

Replace the line

[text drawAtPoint:point withAttributes:nil];

with

NSColor *color = [NSColor greenColor]; \\put something else here...
NSDictionary *attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
[text drawAtPoint:point withAttributes:attributes];

Upvotes: 9

Related Questions