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.
Related
I am writing an app and I want to make a textfield background turn white.
So far this is my code:
- (void)viewDidLoad
{
UITextField *txtfield = UsernameTextField;
[txtfield setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5] ];
txtfield.layer.borderColor = [UIColor blackColor].CGColor;
txtfield.layer.borderWidth = 1;
txtfield.borderStyle = UITextBorderStyleRoundedRect;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
But this code have a result like this:
and i tried this code , to increast alpha
[txtfield setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.8] ];
still the same
and also i see this post
iPhone UITextField background color
and i tried
but it still the same
Why textfield wont turn to white?
First thing first you should
[super viewDidLoad] should be the first statement in - (void)viewDidLoad after initialising parent child should do initialise.
Second
Please check the order of views put on view controller bring UsernameTextField to top of all views or you can call [self.view bringSubviewToFront:txtfield] in code
Third
Only by setting alpha to 1.0 you can see complete white background.
and finally is you still don't get it resolved then there is some changes you did in xib/storyboard view. So to reset just delete textfield and add it again.
In order to change background color it's important to set the border to None
txtfield.borderStyle = UITextBorderStyleNone;
Details: Changing background colour within a TextField in Interface Builder
iPhone UITextField background color
From Apple Doc:
When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the text field. Images you use for the text field’s background should be able to stretch to fit.
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]];
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.
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.
I wanted to put a background image partially visible behind a list view. I created a list view in my nib with an image view behind it and made the list view background 20% opacity. This allows the background image to show thru but my text in the cells show a white background behind and if I create the cells in cellForRowAtIndexPath the cells have a white background too.
I am creating and setting the cell textLabel and the detailTextLabel in the cellForRowAtIndexPath event.
Not sure how to get rid of that, or if there is a better way to do the background image.
I had a similar issue and resolved it by setting the cell.contentView.backgroundColor to [UIColor clearColor] in cellForRowAtIndexPath.
If you did not want to make the whole cell transparent but the labels only, you can set the backgroundColor attribute of the textLabel and detailTextLabel to clear as well:
cell.textLabel.backgroundColor = [UIColor clearColor];
To set the background image of a table at run time rather than in a nib you can create a new UIColor from an image. Like so:
UIImage *img = [UIImage imageWithContentsOfFile: someFilePath]; // get your background image
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img];
[myTable setBackgroundColor: backgroundColor];
[backgroundColor release];