如何更改自定义NSTextfield的drawRect中的文本颜色

Elf*_*ros 2 cocoa objective-c interface-builder nstextfield

我使用NSTextfield的自己的类OMNTextfield子类来显示文本,并在文本长度大于控件宽度时为文本设置动画.

该文字在iTunes中像歌曲标题一样动画.它就像一个魅力!

现在我想更改文本的颜色和文本对齐方式.我已经搜索了一会儿setTextcolor,setAlignment,viewWillDraw,cellClass,...但没有任何工作=>我的文字仍然是黑色的!

你可以在这里找到完整的代码(Xcode项目)

我的问题:如何更改NSTextfield子类中的文本颜色/对齐方式?

Acoording 这个职位代码波纹管应该工作,但它不会!

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

完整代码:

//
//  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
Run Code Online (Sandbox Code Playgroud)

PS:我的代码基于Dave Delong代码:iTunes Song Title Scrolling in Cocoa

Nat*_*ler 9

更换线

[text drawAtPoint:point withAttributes:nil];
Run Code Online (Sandbox Code Playgroud)

NSColor *color = [NSColor greenColor]; \\put something else here...
NSDictionary *attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
[text drawAtPoint:point withAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)