Equivalent of clipToBounds for Mac (NSView's layer) - macos

I have a class "GlobalView" which extends NSView and has two subviews GreenRectView (which extends NSView).
I use the CALayer from my GreenRectView in order to be able to rotate (with setAffineTransform:) my view around a point other than the default bottom-left point. Thus I used "setAnchorPoint". Everything works well except that when my green rect is rotated , it is not draw outside the bounds of its view. In iOS it is possible to use clipToBounds:NO to accept that drawing is display outside bounds but in Mac it doesn't work with masksToBounds... How can I rotate my greenrect and display it entirely whatever the angle of rotation.
See picture :
And here is the code :
#implementation GlobalView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
g = [[GreenRectView alloc]initWithPoint:NSMakePoint(200, 200)];
[self addSubview:g];
g = [[GreenRectView alloc]initWithPoint:NSMakePoint(100, 400)];
[self addSubview:g];
}
return self;
}
#import <Quartz/Quartz.h>
#include <ApplicationServices/ApplicationServices.h>
#implementation GreenRectView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setWantsLayer:YES];
layer = [self layer];
[self setLayer:layer];
[layer setAnchorPoint:NSMakePoint(0.0,0.5)];
[layer setMasksToBounds:NO];
[menuLayer addSublayer:layer];
}
return self;
}
- (id)initWithPoint:(CGPoint)p {
return [self initWithFrame:NSMakeRect(p.x, p.y, 120, 100)];
}
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor greenColor] setFill];
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0, self.frame.size.width, self.frame.size.height) xRadius:5.5 yRadius:5.5];
[path stroke];
}

Related

Transparent NSWindow + custom NSView - don't want custom drawn objects to cast shadows?

So I have a borderless window with it's background color set to clear. Inside it, I have an NSView with some custom drawing. It looks like this:
https://www.dropbox.com/s/16dh9ez3z04eic7/Screenshot%202016-02-10%2012.15.35.png?dl=0
The problem is, the custom drawing is casting a shadow, which you can see if the view is hidden:
https://www.dropbox.com/s/04ymp0b2yqi5egu/Screenshot%202016-02-10%2012.19.04.png?dl=0
I only want the shadow around the frame of the window! How can I achieve this?
NOTE: strangely enough, giving the window a title bar provides the behavior I want: https://www.dropbox.com/s/1vv9iwb5403tufe/Screenshot%202016-02-10%2012.25.29.png?dl=0 - Unfortunately, I don't want a title bar.
The code:
#implementation MyWindow
/*
In Interface Builder, the class for the window is set to this subclass. Overriding the initializer
provides a mechanism for controlling how objects of this class are created.
*/
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag {
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:NO];
if (self != nil) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self makeKeyAndOrderFront:NSApp];
[self setMovable:YES];
[self setShowsResizeIndicator:YES];
[self setResizeIncrements:NSMakeSize(2, 2)];
[self setLevel:TOP_LEVEL];
}
return self;
}
/*
Custom windows that use the NSBorderlessWindowMask can't become key by default. Override this method
so that controls in this window will be enabled.
*/
- (BOOL)canBecomeKeyWindow {
return YES;
}
#end
And the ContentView:
#implementation WindowContentView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
// we need to redraw the whole frame
dirtyRect = self.bounds;
// background
NSBezierPath *framePath = [NSBezierPath bezierPathWithRoundedRect:dirtyRect xRadius:0.0f yRadius:0.0f];
[[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:0.3f] setFill];
[framePath fill];
// border
[[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:1.0f] setStroke];
framePath.lineWidth = 1.0f;
[framePath stroke];
// grid
[self drawGridInRect:dirtyRect];
[super drawRect:dirtyRect];
}
- (void)drawGridInRect:(NSRect)rect {
int rows = 5;
int columns = 5;
float gridWidth = CGRectGetWidth(rect)/columns;
float gridHeight = CGRectGetHeight(rect)/rows;
[[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:0.2f] setStroke];
[NSBezierPath setDefaultLineWidth:1.0f];
for(int i=1; i<rows; i++) {
[NSBezierPath strokeLineFromPoint:NSMakePoint(gridWidth*i, CGRectGetMaxY(rect))
toPoint:NSMakePoint(gridWidth*i, CGRectGetMinY(rect))];
}
for(int i=1; i<columns; i++) {
[NSBezierPath strokeLineFromPoint:NSMakePoint(CGRectGetMaxX(rect), gridHeight*i)
toPoint:NSMakePoint(CGRectGetMinX(rect), gridHeight*i)];
}
}
#end

Custom NSButtonCell, drawBezelWithFrame not called

I'm trying to figure out how to custom draw Buttons in Cocoa/OSX. Since my view is custom drawn I will not use IB and want to do it all in code. I created a subclass of NSButtonCell and a subclass of NSButton. In the Subclass of NSButtonCell I override the Method drawBezelWithFrame:inView: and in the initWithFrame Method of my subclassed NSButton I use setCell to set my CustomCell in the Button. However, drawBezelWithFrame gets not called and I don't understand why. Can someone point out what I've done wrong or what I miss here?
Subclass of NSButtonCell:
#import "TWIButtonCell.h"
#implementation TWIButtonCell
-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
//// General Declarations
[[NSGraphicsContext currentContext] saveGraphicsState];
//// Color Declarations
NSColor* fillColor = [NSColor colorWithCalibratedRed: 0 green: 0.59 blue: 0.886 alpha: 1];
//// Rectangle Drawing
NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRect: NSMakeRect(8.5, 7.5, 85, 25)];
[fillColor setFill];
[rectanglePath fill];
[NSGraphicsContext restoreGraphicsState];
}
#end
Subclass of NSButton:
#import "TWIButton.h"
#import "TWIButtonCell.h"
#implementation TWIButton
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TWIButtonCell *cell = [[TWIButtonCell alloc]init];
[self setCell:cell];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
#end
Usage:
- (void)addSendButton:(NSRect)btnSendRectRect
{
TWIButton *sendButton = [[TWIButton alloc] initWithFrame:btnSendRectRect];
[self addSubview:sendButton];
[sendButton setTitle:#"Send"];
[sendButton setTarget:self];
[sendButton setAction:#selector(send:)];
}
Following are the things seems to be missing out from your code.
You are not calling the [super drawRect:dirtyRect]
You are not overriding + (Class)cellClass in the Class(TWIButton) which is derived from NSButton.
Below is the code after changes:
#implementation TWIButton
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TWIButtonCell *cell = [[TWIButtonCell alloc]init];
[self setCell:cell];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
//Changes Added!!!
[super drawRect:dirtyRect];
}
//Changes Added!!!!
+ (Class)cellClass
{
return [TWIButtonCell class];
}
#end
Now keep the break point at drawBezelWithFrame and check it will get called.
One might forgo the NSButton subclass, since it looks like you are ONLY using it to instantiate the Cell type within the initializer.
Simply
NSButton *button ...
[button setCell: [[TWIButtonCell alloc] init] autorelease]];
btw. you will probably have a leak in the previous examples since you init and then call setCell that likely has its own retain.

drawRect not called on PDFView subclass

I have a custom PDFView subclass and have overridden -mouseDown, -mouseDragged etc to draw a rectangle over the view. Here is the code I am using:
#implementation MyPDFView {
NSPoint clickLocation;
NSRect selection;
}
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
clickLocation = [self convertPoint:clickLocationOnWindow fromView:nil];
NSLog(#"%#", NSStringFromPoint(clickLocation));
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSPoint mouseLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
NSPoint currentLocation = [self convertPoint:mouseLocationOnWindow fromView:nil];
CGFloat lowerX = fmin(clickLocation.x, currentLocation.x);
CGFloat lowerY = fmin(clickLocation.y, currentLocation.y);
CGFloat upperX = fmax(clickLocation.x, currentLocation.x);
CGFloat upperY = fmax(clickLocation.y, currentLocation.y);
selection = NSMakeRect(lowerX, lowerY, upperX-lowerX, upperY-lowerY);
[self setNeedsDisplay:YES];
NSLog(#"%#", NSStringFromRect(selection));
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
NSLog(#"drawRect");
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:selection];
[[NSColor blueColor] set];
[bp fill];
}
#end
The NSRect is calculated correctly, but when I call [self setNeedsDisplay], -drawRect is never called, and the rectangle is never drawn.
Is there any reason why -drawRect is never called on a PDFView subclass?
I have a similar use case. According to the docs, override PDFView's drawPage method instead. Continue to call setNeedsDisplay on the PDFView. It works, but it's a little slow. Working on overlaying a view right now instead.

setNeedsDisplay for DrawRect after button press

I have this in a custom class on my NSView and when I press a button using my drawMyRec IBAction I want to change the color of my NSRect however this is not working, can anyone help?
#import "myView.h"
#implementation myView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
RectColor = [[NSColor blackColor] init];
}
return self;
}
- (IBAction)drawMyRec:(id)sender;
{
NSLog(#"pressed");
RectColor = [[NSColor blueColor] init];
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSRectFill(dirtyRect);
[RectColor set];
}
#end
Your drawRect function is incorrect. change to this:
- (void)drawRect:(NSRect)dirtyRect
{
[RectColor set];
NSRectFill(dirtyRect);
}
First, why are you calling -init on an NSColor? Second, you're setting the color after you've drawn the rect, so it won't take affect until the next redraw. 3rd, what's dirtyRect when -drawRect: is called? Why not just fill the entire rect regardless of what's dirty?

Rounded corners for NSView subclass not working

I've got a custom NSView subclass which I want to have rounded corners. I use the following code in the .m file:
#import "ItemImageSelectionView.h"
#implementation ItemImageSelectionView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.wantsLayer = YES;
self.layer.frame = self.frame;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.0 green:0.5 blue:1 alpha:1] set];
NSRectFill(dirtyRect);
[self.layer setCornerRadius:5.0];
}
#end
I use this code to initialize the view, pretty default:
NSView *imageSelectionView = [[ItemImageSelectionView alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - 2, imageView.frame.origin.y - 2, imageView.frame.size.width + 4, imageView.frame.size.height + 4)];
[self addSubview:imageSelectionView positioned:NSWindowBelow relativeTo:imageView];
But it doesn't set any rounded corners! What am I doing wrong?
You don't need to set rounded corners in the drawrect method; set that in the init. Also, you should probably call the super method, [super drawRect:dirtyRect] in your overrided method. I'm not sure how your fill custom will interact with the layers, though. You may need to set [self.layer setMasksToBounds:YES];

Resources