![](/img/trans.png)
[英]How to implement a custom Focus Ring in drawRect for NSTextField or NSTextVew
[英]How to change the text Color within the drawRect of a custom NSTextfield
我使用NSTextfield的自己的類OMNTextfield子類來顯示文本,並在文本長度大於控件寬度時為文本設置動畫。
該文字在iTunes中像歌曲標題一樣動畫。 它就像一個魅力!
現在我想更改文本的顏色和文本對齊方式。 我已經搜索了一下setTextcolor,setAlignment,viewWillDraw,cellClass,...但沒有任何工作=>我的文字仍然是黑色的!
我的問題:如何更改NSTextfield子類中的文本顏色/對齊方式?
根據這篇文章 ,下面的代碼應該可以工作,但事實並非如此!
-(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];
如上所述,Nate Chandler使用NSDictionary *屬性作為文本顏色。
關於文本對齊,我設法通過計算drawAtPoint:point的良好值來居中文本
我們擁有所有需要的信息stringWidth和控制NSTextfield寬度來計算drawAtPoint中的變量點的良好值:point
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.