Re: placeholder text in NSTextView
Re: placeholder text in NSTextView
- Subject: Re: placeholder text in NSTextView
- From: Philippe Mougin <email@hidden>
- Date: Fri, 8 Apr 2005 16:08:29 +0200
Matt DeFoor wrote:
> Is it possible to have placeholder text in an NSTextView
> similar to that which is available with an NSTextView?
> If so, any pointers how to get started doing that?
Here is how I implemented this feature recently. So far it seems to
work correctly.
The idea is to create a subclass of NSTextView (let's call it
"TextViewWithPlaceHolder") with the following implementation:
static NSAttributedString *placeHolderString;
@implementation TextViewWithPlaceHolder
+(void)initialize
{
static BOOL initialized = NO;
if (!initialized)
{
NSColor *txtColor = [NSColor grayColor];
NSDictionary *txtDict = [NSDictionary
dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName,
nil];
placeHolderString = [[NSAttributedString alloc]
initWithString:@"This is my placeholder text" attributes:txtDict];
}
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return [super becomeFirstResponder];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
if ([[self string] isEqualToString:@""] && self != [[self window]
firstResponder])
[placeHolderString drawAtPoint:NSMakePoint(0,0)];
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return [super resignFirstResponder];
}
@end
----
Best,
Philippe Mougin
Open source scripting layer for Cocoa: http://www.fscript.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden