First i need some background color on the text only. Like the headers in the F-Script Browser
Setting [cell setBackgroundColor: [NSColor blueColor]]; colors the whole cell space not only the text. Also i would need underlined and strikeout text. And to make things readable i would finally like to change the colors (foreground/background) of the selection on the styled items.
Can i do this with the default NSTextFieldCell ?
Nearly all of these things are jobs for NSAttributedString. As long as the text field cell has rich text enabled, it should accept an attributed string as its object value just fine.
As for the selection color, remember that an NSTextFieldCell is a kind of NSActionCell, and an NSActionCell is a kind of NSCell. NSCells have a method you can override to return a different highlight color. That covers the background; for the foreground, you may have to draw at least the highlighted portion yourself. You may be able to make good use of blend modes in that method.
Related
I have NSCollectionView with custom items which contain NSTextField and NSImageView.
I realized highlight behavior of collection view items manually (ie. redefined the setSelected: method in my NSCollectionViewItem descendant class).
I successfully change the background color of selected items, but I miss one small but important thing: the text color of the selected item doesn't change.
I know that the NSTableView item highlighting changes the text color along with the background color, but I cannot mimic it since I don't know the algorithm of color's change.
The text color of my NSCollectionViewItems can be different. When I highlight the item in NSTableView, the gray text becomes lightgray, black becomes white and so on.
Does anybody know how they do that? Maybe there's a ready solution in the Cocoa API that I missed? Please help.
I would like to individually set cell background colors in an NSForm. I thought it would be a matter of subclassing NSForm and overriding -drawCellAtRow:column:. But -drawCellAtRow:column: only gets called when the cell receives focus.
I've experimented with calling -setCellBackgroundColor on the NSForm but that changes the title background, not the value.
Is there an approach I'm not finding?
I think this just isn't possible with NSFormCell.
First thing I tried:
self.form.drawsCellBackground = YES;
self.form.cellBackgroundColor = [NSColor redColor];
This appears to draw the background only behind the label. Some further testing suggested that a single NSFormCell spans both the label and text field, and the background was drawing behind both. When the text field draws on top, its own background covers up the red color.
Next I subclassed NSFormCell. The only method to be called is -drawWithFrame:inView:. The frame, again, spans both the label and text field. The control view is the NSForm. If I draw my own background and then invoke [super drawWithFrame:inView:], it appears just as above, visible behind the label, but not visible behind the text field. If I invoke super and then draw the background, it appears atop both.
NSFormCell's implementation does not invoke -drawInteriorWithFrame:inView: nor does it seem to provide another override point.
I tried setting highlighted, and implementing -highlightColorWithFrame:inView: – same issue.
While I was trying to understand how this all worked I also tried subclassing NSForm. The only drawing-related method called is -drawRect:. These methods are never invoked: -drawCellAtIndex:, -drawCellAtRow:column:, -drawCell:, -drawCellInside:.
Apart from getting Apple to fix this, the only solutions I see are trying to use NSForm's superclass NSMatrix, or just using labels and text fields.
I've got a NSTextView where I'd like to control drawing of the highlight/selection. Anyone know which method I have to overwrite so I can control what to draw in the selection rect?
You do not specify what you want to draw, so it's not easy to give you a straight answer.
If it's just an attributed string (Font, Style, Color, Background), you do not need anything fancy, just look for NSAttributedString. There are methods like -setSelectedTextAttributes: and the delegate method -textViewDidChangeSelection:
Generally, you should not subclass NSTextView if you don't have a very good reason to do so. You can do almost anything by just utilizing the usual delegation mechanisms.
If it's something very customized, there is a variety of possibilities depending on if you want to customize the selection (-setSelectedRange:) draw an overlay view (get the frame of the selectedRange) or mess around with the low level layout engine (-layoutManager). It really depends.
If you don't want to use the standard attributes to highlight text, eg by using an CALayer, you can get the rects containing the selection from the layoutManager of the NSTextView.
I am using a custom editor when editing the contents of a cell inside a Table view. From the docs I see that the custom editor has to be an NSTextView. So I put the text view in the document view, then I referenced it from the cell subclass through an IBOutlet in order to assign it as custom editor.
When doing all this, I can set the editor not to draw its background, but a thick white border is shown when the user edits that cell, and there is no way to remove it.
So I create the NSTextView programmatically, assigned it as the custom editor, and no white border is shown, but I can't change the background now, set it to clearColor, set the font, fontColor etc. I can't do anything with it. It is just a square with dark background and white text.
Is there something I am not doing? This is a bad approach?
Thank you.
The border is drawn by drawRect:, but I still don't know how to fix it by overriding drawRect:...
You may have a look at this link: http://www.cocoabuilder.com/archive/cocoa/129091-solved-re-disabling-nstableview-big-black-editing-box.html#129259
I've got an column of cells in an NSTableView that get their text color from the app's preference plist. I want to set the text color to white when highlighted, but have been unable to figure out a good way to do this.
Anybody got any ideas?
Assuming there's no easier way to do this, implement the tableView:willDisplayCell:forTableColumn:row: delegate method to set the cell's textColor to either [NSColor alternateSelectedControlTextColor] or [NSColor selectedControlTextColor] depending on whether rowIndex is in the table view's selectedRowIndexes set.
(The “selected” in “{alternateS,s}electedControlTextColor” refers to the control, not the text. You're using the alternate (or not alternate) text color for the selected control, which is the table view.)