Scroller's background at NSTableView - xcode

How can I change background color of scroller at NSTableView? For example I've made black background color of my TableView and got white scroller, I want ClearColor or Black color of Scroller's background. How can I make it?

You'll need to subclass NSScroller and override its -(void)drawRect: method
- (void)drawRect:(NSRect)dirtyRect
{
// the only line is required
[self drawKnob];
}

Related

How do I tint an application window like this one?

I am very new to OSX development.
How do I tint an application to dark gray like this one did?
is it possible to do that from interface builder? If not, how do I do that from code?
To change window title bar color, set the window as textured in IB. Then in corresponding view controller/app delegate update the window color.
NSColor *grayColor = [NSColor colorWithCalibratedRed:64/255.0f
green:64/255.0f
blue:64/255.0f
alpha:1.0];
[self.window setBackgroundColor:grayColor];
Now the window would like this :
With Toolbar:
Note that when window is set as textured entire window will be textured. I have subclassed the view and set background to white color :
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
}
Else the window would like this :

Update NSView Background color in fullscreen

I want to set a background color to a view and as of now I have subclassed that view in Xib and in drawRect method I am setting the color.
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
Am using [self.view enterFullScreenMode:screen withOptions:nil]; to show the view(which contains an image) in fullscreen. But when view is in fullscreen it shows a default gray background instead of black color. How can I set the background to black ?
Are you sure your methods calling? Put a NSLog to make sure.
If it is, then delete the line [super drawRect:dirtyRect];

NSTableView with rounded corners

I'm trying to customize the UI of my application and I want my NSTableView to have rounded corners. So I subclassed NSTableView and got this:
However, when I populate the table and select a row, the selection is drawn over the border like this:
I've tried adding a clip in the table view drawing code and it doesn't work. Any suggestions for how I can fix this?
Edit:
My drawing code in the NSTableView is the following:
- (void)drawRect:(NSRect)dirtyRect {
[NSGraphicsContext saveGraphicsState];
NSRect frame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
[[NSBezierPath bezierPathWithRoundedRect:frame xRadius:3.6 yRadius:3.6] addClip];
[super drawRect:dirtyRect];
[NSGraphicsContext restoreGraphicsState];
}
The actual rounded frame is drawn in the NSScrollView drawRect method. The interesting thing is that this does clip the selection of the very first and very last rows:
But not when the table is scrolling:
So the question remains: how can I clip all drawing inside the rounded frame of the NSScrollView?
I found that you can call this on the container scroll view of the table view.
self.scrollView.wantsLayer = TRUE;
self.scrollView.layer.cornerRadius = 6;
That's all I needed and it works. No subclassing needed.
I was able to solve this pretty nicely using CALayer. After trying subclassing everything from NSScrollView to NSTableView to NSClipView, and still getting the rendering problems shown above, I finally simply added this code to the drawRect of the NSScrollView subclass:
if (!self.contentView.wantsLayer) {
[self.contentView setWantsLayer:YES];
[self.contentView.layer setCornerRadius:4.0f];
}
And then I draw the frame in the same drawRect method of the NSScrollView. It solves all the problems above.

Text color of extra label in view-based NSTableView

In a view-based NSTableView, your custom row and cell views (subclasses of NSTableRowView and NSTableCellView) get their backgroundStyle property set, so you know if the background is light or predominantly dark (for the selected, highlighted row).
This even gets passed to immediate subviews.
Now, the default text label of the table cell view reacts correctly to this, so on a dark background, the text is drawn in a suitable light color.
However, an NSTextField added to provide extra text (with a custom text color set in Interface Builder) does not automatically adhere to this convention.
Is there a simple way in the API to get the text field to play nice, or do I have to subclass it?
Instead of overriding drawRect, you could also do this:
- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
NSColor *textColor = (backgroundStyle == NSBackgroundStyleDark) ? [NSColor windowBackgroundColor] : [NSColor controlShadowColor];
self.detailTextField.textColor = textColor;
[super setBackgroundStyle:backgroundStyle];
}
See also here: http://gentlebytes.com/blog/2011/08/30/view-based-table-views-in-lion-part-1-of-2/
Just subclass NSTableCellView then implement drawRect:
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
if (self.backgroundStyle == NSBackgroundStyleDark) {
[yourTextFieldIVar setTextColor:[NSColor whiteColor]];
} else if(self.backgroundStyle == NSBackgroundStyleLight) {
[yourTextFieldIVar setTextColor:[NSColor blackColor]];
}
}

NSTextView rounded border / stroke in Cocoa

I am subclassing NSTextView and over-riding the drawRect method in order to draw an NSBezierPathWithRoundedRect around the textview, however - as it has rounded edges, they interfere with the text in the text view.
Is there any way to apply some kind of margin or padding all around the text input area of the NSTextView so that it is more inset, and away from the rounded edges? Or is a better approach to sit the NSTextView within an NSView and apply the rounded stroke to the NSView instead?
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
[super drawRect:dirtyRect];
NSRect rect = [self bounds];
NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);
NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:10 yRadius:10];
[textViewSurround setLineWidth:2.0];
[[NSColor whiteColor] set];
[textViewSurround stroke];
}
I think -setTextContainerInset: does what you need. Alternatively, you should not call super in your implementation of -drawRect: and draw the text container yourself.
You could try setting margins on the paragraph style of the whole textView's string.
Or maybe put your textview inside a larger parent view with rounded corners and a border?

Resources