Objective-C NSScrollView Foreground Colour - objective-c

I am getting into app development and just started using NSScrollViews to display large chunks of text.
I am able to set the background colour by writing the following:
[_HeadersScrollView setBackgroundColor:[NSColor darkGrayColor]];
But not set the foreground colour by doing something similar to:
[_HeadersScrollView setTextColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForegroundColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForeground:[NSColor whiteColor]]; // nope
Is there a method or any other way that I could get this type of setup to work? I'd really appreciate it.

The NSScrollView contains an NSClipView (a helper for scrolling) which contains the NSTextView. If you want to operate on the text view you either need an outlet to that or you can request the documentView from the scroll view.
You can do either:
[_HeadersScrollView.documentView setTextColor:[NSColor whiteColor]];
Or, if you have an outlet to the text view (called _textView in my example), you can do:
_textView.textColor = [NSColor whiteColor];
In this case, you would probably want to set the background color on the text view, too, rather than the scroll view. And tell it to draw its background by setting drawsBackground.

There is no setting like that for changing foreground color in scrollview.
_HeadersScrollView.tintColor = [NSColor whiteColor];
or
[_HeadersScrollView setTintColor:[NSColor whiteColor]];

Related

Setting UITableViewCellStyle2 Tint Color

I can't believe I don't see this anywhere on Google, but I have an odd issue.
I am changing the Global Tint in iOS 7 in the App Delegate:
[[UIView appearance] setTintColor:[UIColor redColor]];
However, when I display a table view with UITableViewCellStyle2 cells, the text title color is still the default blue color.
Any ideas on how I could fix this? I'd rather not subclass it.
Thanks!
simply change the text color.
cell.textLabel.textColor = [UIColor redColor];

NSTextView inherits parents view background on update

I have a NSTextView that I am using to display a timer. This timer updates every second counting down to 00:00. When the timer updates the string for the NSTextView, the NSTextView acts as if it redraws but uses its parents views background for its own background.
My NSTextView has a clear background color and has been set not to draw its background. Its parent view draws a 3 part image for its own background. Below is the code for the NSTextView.
_timerLabel = [[NSTextView alloc] init];
[self.timerLabel setSelectable:NO];
[self.timerLabel setEditable:NO];
self.timerLabel.font = fontLoaded ? [NSFont fontWithName:#"DS-Digital" size:26.0f] : [NSFont fontWithName:#"Lucida Grande" size:26.0f];
self.timerLabel.textColor = [NSColor colorWithCalibratedRed:(50.0f/255.0f) green:(50.0f/255.0f) blue:(50.0f/255.0f) alpha:1.0f];
self.timerLabel.backgroundColor = [NSColor clearColor];
[self.timerLabel setDrawsBackground:NO];
[self.timerLabel setAllowsDocumentBackgroundColorChange:NO];
[self.timerLabel setAlignment:NSCenterTextAlignment];
self.timerLabel.string = #"5:11";
[self addSubview:self.timerLabel];
Any ides why this would be happening? I have tried everything I could think of or find in the apple documents and nothing has solved my issue. Any help would be greatly appreciated.
Before updating the text in the NSTextView
https://www.dropbox.com/s/za3twd8hb7r1apr/Screen%20Shot%202013-04-10%20at%205.37.10%20PM.png
After updating the text in the NSTextView
https://www.dropbox.com/s/p0bsk63o8yweyqs/Screen%20Shot%202013-04-10%20at%205.37.28%20PM.png
I figured out what was happening and thought I share in case anyone else gets stuck with the same issue.
When updating the text for the NSTextView (can be an NSTextField too), the drawRect function gets called with the rect for the NSTextView that was updated.
In my case, my draw rect was drawing a 3 part image for the background of my NSView to the supplied rect in the drawRect function. Since the rect was the rect of the NSTextView after and updated to its string, this would redraw my background image for the size of the NSTextView.
Currently the only solution I have been able to come up with is to always draw my 3 part image for the bounds of my NSView.
Hope that makes since.

Transparent NSWindow, but not subviews?

Is it possible to have a transparent NSWindow, but not for it's subviews (NSTextField and NSButton). Right now the text is also showing wat's below, I'd like that to be 0% transparent.
Here's what I do in the NSWindow init:
[self setAlphaValue:0.9];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
You need to put a dark backgound image having the default colour of window behind each of textfield and button.
For this purpose you can subclass all required objects and add the above.

Custom NSButton with semi-transparent background

I'm trying to create a custom NSButton with a 50% opaque black background and white text. To do this I've subclassed NSButton and overloaded DrawRect:
- (void) drawRect:(NSRect)dirtyRect
{
[self setBordered:NO];
//REMED since it has same effect as NSRectFill below
//[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];
NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
[backgroundColor setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
The white text appears fine but the button's background is always 100% opaque. The alpha value is not interpreted.
Any ideas? Thanks!
The default operation of NSRectFill() is copy which is not what you want. Replace it with
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);
Another solution I found was to keep my code the same but turn on the Core Animation Layer for each button in Interface Builder. I don't know enough about Core Animation Layer to know why this worked. I had previously turned CAL off because it was making my fonts look very jagged.

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.