How can I change the way NSButtonCell objects highlight when clicked? - cocoa

I am using several NSButtonCell objects in an NSTableView. They are simple square buttons with custom images on them.
These buttons draw properly when they are not highlighted: all that is visible is the image, and the rest of the button rectangle is transparent. However, when I click on them, the entire button rectangle is highlighted, inverting the background in the parts that were transparent.
I would prefer to see the image drawn inverted, and the transparent parts remain transparent. How can this be done?

Try setting your cell's highlightsBy property to NSContentsCellMask. I think you'll have to do this in code (probably in awakeFromNib); I don't see a way to do it in IB alone.

You can do it in Interface Builder too. I use "Square Button" so the button alters between two images (so the image is not inverted at all).
Your buttons behaviour is probably set to "Momentary Light" or "Momentary Push In".
Set the Behaviour to "Momentary Change", and it should work.

Related

Xcode invisible elements

I don't know what happened. Elements items like buttons or segmented control are invisible in main storyboard. Cellphone is the same.
I put the image, of what I mean
It looks like you accidentally changed the view's (or window's) Tint Color to white. (Or you could even have turned it to Clear color, or no color at all.)
This would cause the text of the button or segmented control to be invisible, because it is white on white. The segmented control has a border but it would turn white too. So you wouldn't be able to see anything.
There are two places to look to fix this. Look at the Tint popup of your view controller's View in the Attributes inspector.
Also look at the Global Tint popup in the File inspector for the whole storyboard.
[EDIT It turned out to be the Global Tint.]

How to display elements inside NSCollectionView with various shapes

I am a rookie Cocoa guy. I need to design and implement a view which will show collection of labels on Mac OS using Xamarin. These labels will have a text and color associated with them. When shown inside the view, label should expand till it covers whole text and it will be shown with background and foreground colors.
I have attached the picture of this user control on Windows, you can see that labels inside the StackPanel are expanding till they cover the whole text. Hope this gives better idea about my ask.
The $64,000 question is "are these labels controls?" In other words, do you expect the user to click on these to do something, or are they just for display?
If your answer is "just for display", the solution is super simple: Use an NSTextField and programmatically add attributed text (NSAttributedString) to it. Attributed text attaches display properties to runs of text within the field; properties like "background color".
If you want these to be buttons that you can click on, then things get a lot more complicated.
Since you apparently want the button layout to "flow", you might look into imbedding buttons (well, button cells) into an NSTextField using attachments. This is normally how non-text content (say, an image) can be inserted, but with some fiddling it can actually be anything a control cell can draw. See How to insert a NSButton into a NSTextView? (inline).
Warning: this is not a "rookie" topic and will involve control cells and custom event handling.
If I were doing this, I'd probably just create NSButton objects for each label (choosing an appropriate style/look like NSRecessedBezelStyle), create a custom subclass of NSView to contain them, and then override the layout method to position all of the buttons the way I want.
To be thorough, I'd also override the intrinsic size methods so the whole thing could participate in auto-layout, based on the number and size of buttons it contained.

NSOutlineView - Disclosure Triangle Interferes with Highlight Rectangle

I have an NSOutlineView with the highlight mode set to source list and the menu property set to a non-empty menu (I figured this last consdition is necessary to have the outline drawn).
When I right-click on a row representing an item that has children (i.e., is expandable), The blue outline around the cell has a slightly different color right above and below the disclosure triangle:
(This happens for every node, at every level of the hierarchy)
Additional information: My outline view is view based, does not use bindings (view controller is the delegate and data source).
Me cells are custom, designed on the storyboard, nothing fancy (icon image view and text field).
What can be causing this?
EDIT: The issue only appears with the round-cornerered highlight rectangle of the "Source List" highlight mode. With the straight-cornered rectangle of the "Regular" highlight mode, the stroke color is even all along.
When using the Source List style, your outline view has an NSVisualEffect view behind it, which causes the list to be composited differently. What you're seeing seems to be a bug with the vibrancy appearance. You could perhaps try to work around it by reducing the frame of the outline cell by overriding frameOfOutlineCell.

Confused with NSButton Image

I have a square NSButton and an Image the same size of the button. I set the image to the button, turned off bordered and set scaling to none. Then the image seems smaller than the button. If I set scaling to axes independent, the image can fill the all button. But is set button enable to false. The button disappeared. Does anyone know what's wrong with it?
Well... Your description, IMHO, is a bit confused! Anyway, for what I understood, you're having problem with Xcode.
You have to set the scaling because the button is always a bit larger than its content, because of the border (even if you turn off its borders, it will still be rendered with a small invisible border). For an unknown reasons, setting to Axes Independently solves this problem and resizes the image in the correct way (this works in any kind of NSControl you can use).
I cannot figure out how enabling or disabling the button can influences its visibility. Are you sure you're not using the "Hidden" property instead of the "Enabled" one?
[myButton setHidden:YES]
This will make the button transparent and insensible to clicks.
[myButton setEnabled:NO]
This will act on the answer to user clicks (and it's what you're looking for). Is your image mainly grey? Disabling the button will make the button gray, and this could make the image invisible...

Making an NSButton resize its image (Cocoa OSX)

I could not find a way in the documentation to tell an NSButton to resize its image to fill up the whole button. Is there a way to do this programatically?
The closest you'll get is -setImageScaling: ... look up the constants to see how the image will be scaled within the button cell, given its bordered state and bezel type.
If you're looking to replace the standard button entirely with your image (ie, the button cell doesn't draw itself at all - your image serves as the entire visual representation), turn off the border (-setBordered:).
All of these options can be configured in IB as well. A tip: in IB, hover the mouse over any setting in the inspector panel - most if not all give you a hint that shows what method controls the behavior affected by the setting's control.

Resources