I've recently come across a problem with a custom NSButtonCell subclass. We have a dark interface with white text, and all is going as expected, until we try to make a default button (i.e. assigning a key equivalent of \r). What we get seems mysterious: The bezel draws, but the text doesn't. But if I make the text black, it draws. If I make the text white with a black shadow, only the shadow draws!
The mystery started to unravel when I tried a test with the text set to [NSColor redColor] on a lark. (The "Create" button here has a key equivalent of \r, "Cancel" is Esc.)
What I eventually figured out is that the default button's cell is drawing into a bitmap context, presumably so that the rendered glyphs can be cached for the animated blue pulsing background that the Aqua look gives. That makes total sense, but the surprise is that that bitmap is then drawn with a multiply compositing operation. Hence black shows up and white doesn't, and anything in between shows up darkened.
I've managed to hack a workaround, by taking the text rendering out of -drawTitle:withFrame:inView: and doing it in -drawBezelInFrame:inView: instead. But that gets called repeatedly, so I'll need to manually cache the image. Plus it's just conceptually ugly, because it's specifically the wrong method for rendering the title!
My question: Surely there must be a better way, right? Is there some way to tell the button cell not to automatically use this offscreen rendering path even though the button is default?
Related
I'm presenting a view in an NSPopover, using code based on this sample code.
The view, and all of its sub-views, are layer-backed. There's a single NSImageView, and several non-editable NSTextFields. The text fields backgroundColors are set to [NSColor textBackgroundColor], and their textColors to [NSColor textColor]. In this way, the text is black if one is using the normal theme, and white if one is using the "dark menu bar and Dock" option (which I'll refer to as "dark theme" from now on). This all works fine, and it looks a little somethin' like this:
Light theme:
Dark theme:
The problem comes when I animate the NSImageView up off the view. As it intersects with the NSTextFields, the image appears to blend with the text fields in an unappealing manner. It happens in both light and dark themes, but it's more icky-looking (it's a technical term) in the dark theme. Dig it:
The code to animate it looks basically like this:
CABasicAnimation* positionAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
positionAnimation.fromValue = [NSValue valueWithPoint:fromPoint];
positionAnimation.toValue = [NSValue valueWithPoint:toPoint];
positionAnimation.duration = imageAnimationDuration;
[self.imageView.layer addAnimation:positionAnimation forKey:#"position"];
self.imageView.layer.position = toPoint;
What have I tried? Oh, what haven't I tried?
First off, my own views don't have any kind of NSVisualEffectView going on. But it seems that NSPopover adds that on its own; you can clearly see my desktop bleeding through the popover in the animation above. That's fine; it's actually a nice effect. But, thinking that my NSImageView was trying to be vibrant, I subclassed NSImageView just to return NO from allowsVibrancy. No change in behavior.
Next, I subclassed NSView to return NO from allowsVibrancy, and made the parent view of my view an instance of that. No change in behavior.
My NSTextFields are set with drawsBackground = NO, so I changed them to YES. No change in behavior. Then, leaving drawsBackground = YES, I set both text field's backgroundColors to [NSColor clearColor]. Here's where it gets weird. This does make the weird drawing go away, but it changes the text color of one of the text fields (the smaller one) to black. Wut? See below.
I gave up on the background colors, and started messing with the text colors. I found that if I set the textColor of the text fields to a discrete color (say, [NSColor blackColor] or [NSColor whiteColor], then the weird drawing problem also goes away. It seems only to get weird when using colors which adapt with the theme such as [NSColor textColor]. That's super lame, because the whole point of using something like [NSColor textColor] is that it adapts to the theme. I could probably hack around and figure out what theme is active and set the colors manually, but I really don't want to go that route if I can help it.
I promise there's a question in here somewhere, and, mercifully, here it is:
How can I fix the animation issue shown above, while still using colors which properly adapt to the current theme?
Sample project on GitHub.
Edit:
The desired result is to have no blending between the image and the text. Something like this:
The image I used in the sample app here maybe isn't the best example to convey the sheer yuckiness of the animation I'm seeing in my actual app. The image in the sample is already mostly white, while in my actual app it's mostly black, and it truly looks horrible when blended with white text.
Visual Effect Views (like used in NSPopover) are totally messed up in OS X Yosemite and they are causing your problem here. It is not your fault, these views are totally buggy.
A workaround on Yosemite should be to set the appearance property of each NSTextField to NSAppearanceNameAqua. Because if the labels don't try to do some weird vibrancy effect, they can't mess things up. The labels still look the same and the strange effect is gone.
My words in code:
self.titleLabel.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
self.descriptionLabel.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
Luckily NSVisualEffectView's are working fine now in El Capitan. So your code should work fine there..
I have an NSView with white text against a black background. The view animates slowly up and down (+/- 10 pixels every 1.5 seconds), and the text visibly flickers while animating. It doesn't flicker a lot, but enough that it's noticeable when trying to read it. I've tried animating both with NSAnimationContext and CABasicAnimation.
This doesn't happen with the exact same design/animation on iOS, just on OSX. It might have something to do with the screen refresh rate, but it's driving me nuts and I figured I'd ask in case there was a simple solution.
The effect you describe is even more pronounced if you animate diagonally. I thought it might be diminished if one turned on shouldRasterize, but it's not. It feels as if it is to be an anti-aliasing of the narrow strokes of the font as the image animates through non-integer positions during the animation. The effect is curiously diminished if you use bold fonts. Also, the problem appears to be unrelated to the NSTextField, because if I take an snapshot of the NSTextField, remove the text field, and animate an NSImageView, the flickering is still apparent.
Bottom line, the effect is diminished with bold fonts. It feels like there should be an anti-alias feature one could turn off, but it's escaping me at this point.
I just happened to start using Facebook's Pop Animation framework in my project, and when I implemented a POPBasicAnimation for this animation the flickering seems to have disappeared completely - even for non-bold text.
I'm writing an OS X app (target 10.10, Xcode 6.1) and I'm really confused by my custom NSButtonCell subclass. It seems like there are things going on here that shouldn't. I'm new to OS X programming, so I'm asking if anyone has insight into the inner workings of NSButtonCell.
First, what seems to be working?
I can set the button's image and title. The image appears normally.
The storyboard sets up the button to be Style: Textured and Type: Momentary Change. It's not Bordered, not Transparent, and doesn't allow Mixed State.
List of complaints:
I override -drawTitle:withFrame:inView: to draw the title in a custom color depending on the cell's highlighted. This color should be #cccccc when the cell is not highlighted, but it's actually #d6d6d6.
The button has both image and alternate image. The image that's drawn is never the alternate image, so I override -drawImage:withFrame:inView: to pick the correct image for cell's highlighted. This appears to work, but what the heck, NSButtonCell? How is on/off state different from highlighted? I've tried many of the Type options and none seem to change the fact that pressing the button will momentarily change highlighted, and toggle state.
Speaking of momentarily changing highlighted, it appears that its duration is about as short as possible, so I had to implement a sort of "debouncer" to prevent -drawWithFrame:inView: from being called more frequently than a specified threshold.
My button cell also has properties myBackgroundColor and myAlternateBackgroundColor. I'm not using backgroundColor because I need to be able to draw a custom background shape (filled rect, filled circle, etc). The alternate background color is used when highlighted. The problem here is that the alternate background color should be #93edbf but appears to be #a1eecb! In order to get it to look like #93edbf, I need to set the color to #84ecb2.
So far this has all been about one particular instance of this button cell. But in another instance, the alternate background isn't drawing at all! I've read through the storyboard code and the buttons are as identical as they can be. My view controller code likewise updates both button cells' properties at the same time. Why would one button behave differently from another?
I want the button to highlight on mouse down instead of "momentarily" after mouse up. I haven't yet implemented this in my custom cell. Man, NSButtonCell is really lacking some things. How does something like that happen? Don't the OS X and iOS teams ever talk to each other?
What could it be?
I've already verified that the cell's controlTint is set to NSClearControlTint. I've checked for background filters, compositing filters and content filters on the off chance they had anything to do with this.
I know Apple really wants us to use their native look and feel for UI elements, but I never thought they'd go so far as to force the use of some highlight tint.
I have a NSButton sibling on top of a NSImageView.
Whenever I click the window, there are some rendering issues. It looks like this:
As you can see, the white edges are the problem.
Strangely, this problem even persists if I override drawRect:.
Nothing gets rendered at all, but whenever I click it, those white edges appear.
Also, when the background-image changes, the button gets redrawn and the edges disappear.
Any idea what might cause this?
EDIT
I found out that this actually happens with every single instance of NSView
and it actually clears part of the buffer (you can see the desktop wallpaper):
EDIT 2
I also just found out that this does not happen if I layer-back the windows content-view.
Well, this question was impossible for anyone to answer.
My window had a custom contentView, which was just drawing a view with rounded corners.
Instead of using self.bounds, I used dirtyRect to draw the background.
So when the contentView wanted to redraw the background of the controls that were updated, those rounded corners were cut out.
I'm drawing some custom shapes behind a textview to make it appear it's a notepad. It draws a repeating bitmap across the top, as well as a white background under the text, and a secondary "note" page under the rest to add a little dimension and layering. See the screenshot:
Now, this is with setDrawingCacheEnabled(true) for the TextView this drawable is applied to. With this method call applied, the scrolling is VERY smooth, exactly what I want. Without it, the black background disappears, but the scrolling is very choppy. Any idea on how to cache this view so that scrolling is smooth without destroying my UI?
Seems this function only works with solid colors, as defined in the documentation which I should have read before posting. I find it hard to believe there's nothing that caches complex views and drawables to work as well as that function does, but, whatevs. Live 'n learn.