how to make NSTextField editable - cocoa

I am creating a custom class which is inherited from NSPanel and this panel is my landing screen of the application.
I am adding a NSTextField on it, which is visible on the screen. Problem is that the textfield is not editable.
However, if i create a new cocoa project and run the same addSubview code for textfield, all is good, i am able to edit the textfield.
It seems to me like problem is with my custom panel, but i am not able to hunt it down.
Here is my code:
NSTextField *infoTextField = [[NSTextField alloc] initWithFrame:rect];
[[window contentView] addSubview:infoTextField];
[infoTextField setDelegate:self];
[[infoTextField window] becomeFirstResponder];
[infoTextField setTextColor:[NSColor blackColor]];
[infoTextField setDrawsBackground:YES];
[infoTextField setBordered:YES];
[infoTextField setSelectable:YES];
[infoTextField setEditable:YES];
[infoTextField setEnabled:YES];
[infoTextField setAlignment:NSLeftTextAlignment];
[infoTextField setStringValue:#"What are you doing?"];
[infoTextField release];
I need your help...

Override the method in NSPanel subclass,
- (BOOL)canBecomeKeyWindow {
return YES;
}

Related

How to drag-move NSWindow with a NSImageView on its contentView?

I'm creating a mini window like the iTunes mini window:
It's draggable and have a background image on it.
Then I created a NSWindow's subclass and overrides its initWithContentRect:styleMask:backing:defer: method, and added an NSImageView to it:
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
contentRect.size = CGSizeMake(287, 287);
self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
if (self)
{
[self setMovableByWindowBackground:YES];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];
NSImageView *imageView = [[NSImageView alloc] initWithFrame:(CGRect){CGPointZero, contentRect.size}];
imageView.image = [NSImage imageNamed:#"MiniWindow.png"];
[self.contentView addSubview:imageView];
}
return self;
}
But after I add the NSImageView to window's contentView, the window become undraggable.
How to make the window become draggable again?
Best regards
Create an subclass of NSImageView and override mouseDownCanMoveWindow method:
- (BOOL)mouseDownCanMoveWindow
{
return YES;
}

How to add a NSTextField to a window in the window's init via a method

I want to programatically add several NSTextFields to a window when it loads. I call the below method for each one in the init such as:
- (id)initWithWindow:(NSWindow *)window{
[self addTextField:firstTextField toWindow:window at:20];
}
-(void)addTextField:(NSTextField*)theTextField toWindow:(NSWindow*)theWindow at:(CGFloat)y{
theTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, y, 200, 20)];
[theTextField setBezeled:NO];
[theTextField setDrawsBackground:NO];
[theTextField setEditable:NO];
[theTextField setSelectable:YES];
[[theWindow contentView] addSubview:theTextField];
}
I don't get any errors, not even when I call the setStringValue for one of the NSTextFields. However, they are not visible in the window. Have I missed something simple, or am I trying something that is not allowed?
Thanks
Try like this:-
- (void)windowDidLoad
{
NSRect frameRect = NSMakeRect(10, 20, 200, 20);
NSTextField *theTextField = [[NSTextField alloc] initWithFrame:frameRect];
[theTextField setBezeled:YES];
[theTextField setDrawsBackground:NO];
[theTextField setEditable:NO];
[theTextField setSelectable:YES];
[vw addSubview:theTextField];
[[[self window]contentView]addSubview:vw];
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
If you load your window from xib you shouldn't override -initWithWindow:, use -windowDidLoad instead and add your textfields in this method. But if you create your window programmatically your -initWithWindow: must look like this:
-(id)initWithWindow:(NSWindow*)window {
self = [super initWithWindow:window];
if(self) {
[self addTextField:firstTextField toWindow:self.window at:20];
}
return self;
}

NSTextField not active in NSPopOver

I have a menubar application, which open a popover. That popover contains NSTextField and few buttons. The problem is that the NSTextField is non-selectable, it's impossible to type anything in it. However, it's possible to click on it with mouse right button and paste something. Well, that's definitely odd behavior. Buttons works without any problems in that popover, btw.
Here's the code i use:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSApp activateIgnoringOtherApps:YES];
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setAction:#selector(showPopOver:)];
[statusItem setImage:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"icon" ofType:#"png"]]];
[statusItem setHighlightMode:YES];
}
and:
- (IBAction)showPopOver:(id)sender {
popover = [[NSPopover alloc] init];
[popover setContentViewController:popOverController];
popover.animates = YES;
popover.delegate = self;
[popover showRelativeToRect:[sender bounds]
ofView:sender
preferredEdge:NSMaxYEdge];
}
}
Any ideas what the problem exactly and how to fix it?
Seems is a bug.
http://openradar.appspot.com/9722231

Custom NSView with NSTextField subview drawing issue

So I am drawing a custom window (transparent) with a custom NSView as the contentview, and wouldlike to add an NSTextField to the window as well, however, when I add the NSTextField, I get a weird resizing or redraw of the custom NSView, and I can't figure out what is causing the problem. Both the window and the contentview of the window are subclasses of NSWindow, and NSView, respectively. Also, I have tried to just layer the custom NSView (not set it as the contentview) with no change. Any ideas?
NSWindow *quickEntryWindow = [[TransparentWindow alloc] initWithContentRect:NSMakeRect([[NSScreen mainScreen] frame].size.width/2 - 250, [[NSScreen mainScreen] frame].size.height/2 + 50, 500, 100)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
special:YES];
BorderView *quickEntryBorderView = [[BorderView alloc] initWithFrame:NSMakeRect(0, 0, [[quickEntryWindow contentView] frame].size.width, [[quickEntryWindow contentView] frame].size.height)];
[quickEntryBorderView canDrawConcurrently];
[quickEntryWindow setContentView:quickEntryBorderView];
NSTextField *quickEntryTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, [quickEntryBorderView frame].size.width-20, [quickEntryBorderView frame].size.height-20)];
[quickEntryTextField setAutoresizingMask:NSViewNotSizable];
[quickEntryTextField setBordered:NO];
[quickEntryTextField setDrawsBackground:NO];
[quickEntryTextField setFocusRingType:NSFocusRingTypeNone];
[quickEntryTextField setFont:[NSFont fontWithName:#"Helvetica" size:42]];
[quickEntryTextField setTextColor:[NSColor grayColor]];
[quickEntryBorderView addSubview:quickEntryTextField];
What I get is something that looks like this (it is normal when no text is entered into the NSTextField):
Oh yeah, I know I'm not managing my memory...I'm just trying to get this working. Thanks!

NSTextView Chinese/Unicode Characters

I have a piece of text drawn in my cocoa app but I also need to draw it in a NSTextField and a NSTextView and look exactly the same, they all usually draw fine but if there are unicode or Chinese characters in the text then the NSTextView has large line spaces between the lines, it works perfectly in the nstextfield and when drawing normally.
I have tried every setting that I can find but none affect this issue, has anyone run into this issue before and know of a fix?
You can see the issue in a screenshot at http://cl.ly/3e0U1T2U2G2z341j3O3T
Here is my test code to show the issue
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(5, 50, 100, 100)];
[textField setStringValue:#"え?移転!? TESTTestTer 友達の旦那の店 THIS IS A TEST THIS IS A TEST"];
[textField setBordered:NO];
[textField setBezeled:NO];
[textField setAllowsEditingTextAttributes:YES];
[textField setDelegate:self];
[textField setFont:[NSFont fontWithName:#"LucidaGrande" size:12.0]];
[textField setTextColor:[NSColor colorWithDeviceRed:(74.0/255.0) green:(74.0/255.0) blue:(74.0/255.0) alpha:1.0]];
[[window contentView] addSubview: textField];
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(200, 50, 100, 100)];
[textView setString:#"え?移転!? TEST TestTer 友達の旦那の店 THIS IS A TEST THIS IS A TEST"];
[textView setDelegate:self];
[textView setFont:[NSFont fontWithName:#"LucidaGrande" size:12.0]];
[textView setTextColor:[NSColor colorWithDeviceRed:(74.0/255.0) green:(74.0/255.0) blue:(74.0/255.0) alpha:1.0]];
[[textView layoutManager] setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
[[textView textContainer] setLineFragmentPadding:0.0];
[[window contentView] addSubview: textView];

Resources