Custom NSView with NSTextField subview drawing issue - cocoa

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!

Related

Why is my layer shadow clipped to my NSView's rect?

I'm creating a window containing an NSButton (both window content view & button have wantsLayer = YES), and setting the NSButton's shadowColor, shadowRadius, shadowOpacity and shadowOffset. But my shadow gets clipped to the rect of the NSView. How do I fix this?
NSRect wdBox = NSMakeRect(0,0,100,100);
NSWindow * theWindow = [[[NSWindow alloc] initWithContentRect: wdBox styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask backing: NSBackingStoreBuffered defer: NO] autorelease];
NSView* cv = [[[NSView alloc] initWithFrame: wdBox] autorelease];
cv.wantsLayer = YES;
[cv setLayerUsesCoreImageFilters: YES];
theWindow.contentView = cv;
[theWindow setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
[theWindow setTitle: #"foo"];
NSButton* mView = [[NSButton alloc] initWithFrame: NSMakeRect(10, 10, 100, 80)];
[mView setLayerUsesCoreImageFilters: YES];
[mView setWantsLayer: YES];
mView.layer.masksToBounds = NO;
[mView.layer setShadowColor: [NSColor.redColor CGColor]];
[mView.layer setShadowOffset: CGSizeMake(4, 4)];
[mView.layer setShadowRadius: 8];
[mView.layer setShadowOpacity: 1.0];
[mView setBezelStyle: NSRoundRectBezelStyle];
[mView setTitle: #"bar"];
[theWindow.contentView addSubview: mView];
Here's a picture:
I had similar code before and it used to work until 10.9, but the shadow suddenly got clipped to the view, so I rewrote it to this simple case, but it's still clipped.
There is a bug in Mavericks. If you create an NSButton in XIB and give it a layer and set its shadow it works fine, but if you create one programmatically it clips its shadow.
I suspect the AppKit team did some crazy hacks when they made buttons do fast layer compositing in Mavericks (they won't redraw their backgrounds unless necessary now, for instance), because they tried to make it all happen magically, which is always always a bad idea.
Note that if you make an NSTextField the shadow code works as you'd expect. It's just NSButtons (so far) that I've found have this issue.
Please file a radar.

Layer-Backed view no longer punches hole in transparent NSWindow

I am trying to make a "hole" in an NSWindow using a CAShapeLayer or even just a CALayer.
When using regular NSViews or even layer-backed views, I can override drawRect: using code like this:
[spotImage drawInRect:self.bounds fromRect:NSZeroRect operation:NSCompositeXOR fraction:1.0];
where spotImage is an NSImage with pure white content and some gradations, and the window has a black background with 0.5 alpha. The NSView subclass where this drawRect is defined has a clearColor background.
The end result is a grey window (It is a transparent window with a styleMask of NSBorderlessWindowMask as can be found in many samples.
If I turn the NSView into a layer-backed view, it calls the drawRect methods and works fine.
When I turn this into a layer-hosting view, and again use the same structure (NSWindow > contentView > CustomView) then, the drawInRect method just draws the image. It no longer punches a hole through it.
It is like the layer itself can no longer punch the hole when it is part of a layer-hosting hierarchy.
Here is some sample code:
The custom NSWindow subclass initializer:
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
[self setBackgroundColor: [NSColor lightGrayColor]]; //[NSColor clearColor]];
[self setAlphaValue:0.5];
[self setOpaque:NO];
[self setHasShadow: NO];
[self useOptimizedDrawing:YES];
[self setIgnoresMouseEvents:YES];
}
return self;
}
the code in my applicationDidFinishLaunching method:
PPContentView *thisView = [[PPContentView alloc]
initWithFrame:CGRectInset([self.window.contentView bounds], 50, 50)];
//[thisView setWantsLayer:YES]; enabling this makes things opaque again
[self.window.contentView addSubview:thisView];
thisView.layer.backgroundColor = [NSColor clearColor].CGColor;
//Create custom content
[thisView setNeedsDisplay:YES];
}
and my custom view's drawRect contains:
[[NSImage imageNamed:#"spotFuzzy.png"] drawInRect:self.bounds fromRect:NSZeroRect operation:NSCompositeXOR fraction:1.0];

NSTextView background not resizing appropriately

I have an NSTextView inside an NSScrollView. The scroll view has the auto resize masks for height and width, so it changes size with the window it's in.
The text view is set up much in the way the apple documentation recommends here.
But no matter what settings I put on the text view, I cannot get the background color to resize along with the scroll view.
Here's a picture of what I'm dealing with:
The width works, but not the height.
Here's my setup code for the textview as it is for this picture:
NSTextView *view = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, scrollview.contentSize.width, scrollview.contentSize.height) textContainer:textContainer];
[view setMinSize:NSMakeSize(0.0, scrollview.contentSize.height)];
[view setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[view setVerticallyResizable:YES];
[view setHorizontallyResizable:NO];
[view setAllowsDocumentBackgroundColorChange:YES];
[view setDrawsBackground:YES];
[view setAutomaticLinkDetectionEnabled:YES];
[view setAutoresizingMask:NSViewWidthSizable];
The white background colour isn’t that of the NSTextView, but rather that of its enclosing NSScrollView.
To change it, either select the NSScrollView in Interface Builder/Xcode 4. Or, to do it programmatically, use -[NSView encosingScrollView]:
NSScrollView *scrollView = [textView enclosingScrollView];
[scrollView setBackgroundColor:[NSColor blackColor]];

how to make NSTextField editable

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;
}

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