objective-c - 如何更改自定义 NSTextfield 的绘制矩形内的文本颜色

标签 objective-c cocoa interface-builder nstextfield

我使用 NSTextfield 的自己的 OMNTextfield 子类来显示文本,并在文本长度大于控件宽度时对文本进行动画处理。

文本是动画的,就像 iTunes 中的歌曲标题一样。它就像一个魅力!

现在我想更改文本的颜色和文本对齐方式。我搜索了一段时间 setTextcolor、setAlignment、viewWillDraw、cellClass,...但没有任何效果 => 我的文本仍然是黑色!

You can find the complete code here (Xcode project)

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

协调to this post下面的代码应该可以工作,但事实并非如此!

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

完整代码:

//
//  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:我的代码基于 Dave Delong 代码:iTunes Song Title Scrolling in Cocoa

最佳答案

更换线路

[text drawAtPoint:point withAttributes:nil];

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

关于objective-c - 如何更改自定义 NSTextfield 的绘制矩形内的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12863058/

相关文章:

ios - 使用自动布局设置 UILabel 字体大小的最佳位置

ios - 如何将 NSString 转换为 long double?

swift - Cocoa SCNetworkInterface 获取媒体子类型

xcode - 在 Xcode 中设置约束,以便 NSView 在内容宽度增加时调整大小

ios - 如何在按下 UIButton 按钮时关闭按钮阴影?

iphone - 一个 View 中有多个 UIAlertView : how to create different delegates

objective-c - 如何将 Objective-C 数组转换为 C 数组?

cocoa - 软滚动动画 NSScrollViewscrollToPoint :

iphone - 如何从 XIB 文件加载 Storyboard ?

objective-c - rootViewController 的 self.view 和 self.view .superview 有什么区别?