Default NSRecessedBezelStyle NSButton visual bug? - macos

I have a basic NSRecessedBezelStyle NSButton added via IB to an NSView. Why is the font messed up in its unselected state? Is this normal?
As you can see, when pushed the recessed button looks fine, but unpressed it's solid black with no shadow. Am I missing something really obvious somewhere? I tried messing around with setAttributedTitle and setAttributedAlternateTitle but that yielded odd results with the push on push off mechanic.

That is the expected behavior for NSRecessedBezelStyle with the default "Push On Push Off" Type, bezeled in On state, plain text in OFF, additionally you can change the Type so the bezel is only displayed when hovering, here is the code to make it gray.
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[attrsDictionary setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
[attrsDictionary setObject:[NSFont boldSystemFontOfSize:12.0] forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraph = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraph setAlignment:NSCenterTextAlignment];
[attrsDictionary setObject:paragraph forKey:NSParagraphStyleAttributeName];
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:#"Button" attributes:attrsDictionary] autorelease];
[button setAttributedTitle:str];

Related

Misaligned NSAttributedString in macOS NSStatusItem's button

I would like to display a two-line NSAttributedString as the button title of the NSStatusItem of my macOS app.
However, it seems to move the text up a few pixels and, thus, cut it off. This problem did not occur before macOS Big Sur.
Workaround
With some effort I managed to generate an NSImage of the text and use it as the button's image.
Question
Is there any way to position the NSAttributedString correctly without using an image?
I found a way to workaround this problem, but I don’t know if this way is correct, the code with Objetive-C is as follows
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
CGFloat minMaxLineHeight = (font.pointSize - font.ascender + font.capHeight);
[style setMinimumLineHeight:minMaxLineHeight];
[style setMaximumLineHeight:minMaxLineHeight];
NSRange range = NSMakeRange(0, text.length);
[attriString addAttribute:NSParagraphStyleAttributeName
value:style
range:range];
[attriString addAttribute:NSBaselineOffsetAttributeName
value:#(-3.5)
range:range];

NSButton(Cell) setFont

It seems that NSButtonCell's setFont method is not available anymore from 10.9.
Is there any way (or category) to (re)implement it?
I don't know why Apple forces it's own styles on buttons.
I am trying for 2 days to style my own custom button (I also needed a category to simply change the button's text color - shame).
You can use -setAttributedTitle: of NSButton to set the style of button title.
Sample code:
NSDictionary *attributes = #{NSForegroundColorAttributeName: [NSColor redColor], NSFontAttributeName: [NSFont userFixedPitchFontOfSize:13.0f]};
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:#"Button" attributes:attributes];
[button setAttributedTitle:aString];

Weird font behavior on the NSTextField

I added programmatically NSTextField to my NSView:
NSTextField *projectLabel = [[NSTextField alloc] initWithFrame:frame];
[projectLabel setStringValue:#"projectName"];
[projectLabel setBezeled:NO];
[projectLabel setDrawsBackground:NO];
[projectLabel setEditable:NO];
[projectLabel setSelectable:NO];
[projectLabel setFont:[NSFont controlContentFontOfSize:13]];
projectLabel.autoresizingMask = NSViewMaxXMargin | NSViewMinYMargin;
[self addSubview:projectLabel];
[self setAutoresizesSubviews:NO];
This field was added correctly, but when I change size of view (or even move window to second display), font on field changes very weird (see attached image).
on start
after change of the size
I do not know what I did wrong
I drew this label on drawRect every time, when the size changes.
So, you're manually telling the field to display in its parent view's drawRect:?
Don't do that. It's a subview, so it'll get told to draw in its turn anyway. Just let that happen.

NSPopUpButton text overlaps menu image

I am using the following to create an NSPopupButton programmatically:
...
NSPopUpButton *accessoryView = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 24) pullsDown:YES];
NSFont *aFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
[accessoryView setBezelStyle:NSRecessedBezelStyle];
[accessoryView setFont:aFont];
[accessoryView setShowsBorderOnlyWhileMouseInside:YES];
[accessoryView setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
NSArray *popupItems = [[NSArray alloc] initWithObjects:#"Your Account", #"Sign In", #"Create Account", nil];
[accessoryView addItemsWithTitles:popupItems];
...
Now, when I add the NSPopUpButton to my view, I end up with the button's text overlapping the icon used for the dropdown menu. I have seen this previously when I use NSControl:setAlignment but I am not using this here. Here is the output:
Can anyone see what I'm doing wrong?
Take care,
Jeremy
It just so happens this is a simple fix. (Thanks Beelsebob on irc.freenode.net!) Basically, you need to have the following code:
[[accessoryView cell] setArrowPosition:NSPopUpArrowAtBottom];
in there somewhere. (I added it just below the line to add the menu items.) I had read the API docs on this call a few times before, since I had implemented the same call with a value of NSPopUpNoArrow to remove the arrow as an interim fix, but it didn't make it clear that the proper value being used above would do what it's doing. Problem solved.

Cocoa (Snow Leopard) NSTextView's textStorage -setAttributes:range: removes characters!

I'm not sure what I'm doing wrong. I have a NSTextView and am registered as the delegate for its textStorage attribute. When I receive -textStorageDidProcessEditing:notification: I'm trying to apply attributes to ranges of characters within the text. It certainly does "something" to the characters, but not what I expect... they just disappear!
A heavily distilled code example. This should make sure the second character in the text field is always red:
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
NSTextStorage *textStorage = [textView textStorage];
if ([[textStorage string] length] > 1) {
NSColor *color = [NSColor redColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil];
[textStorage setAttributes:attributes range:NSMakeRange(1, 1)];
}
}
Instead, as I type the sequence "abcdefg" I get "a", then when I hit "b" seemingly nothing happens, then when I hit "cdefg" typing occurs as normal, making the end result "acdefg"... the "b" is missing!
If I start hitting backspace I have to hit backspace 7 times, as if the "b" is actually there, but just not being drawn (cursor stalls as it deletes the "b", then on the next backspace deletes the "a" as expected).
If I apply attributes to some default text in the view using the same -setAttributes:range: method before the view is drawn then it does exactly as I expect.
Any clues? It seems like a fairly normal use of a NSTextStorageDelegate :)
I've tried calling -setNeedsDisplay on the text field to no avail.
Figured it out. Using NSTextStorage's -addAttribute:value:range works. I still don't fully understand why but at least I can get over it and move on.
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
// ... SNIP ...
[textStorage addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(1, 1)];
}
Makes the code a bit less cluttered too.
I'm not sure how relevant this is for you after so many years but I think the reason for it was that you were setting attributes with a dictionary which does not contain NSFontAttributeName, effectively removing it from the textview.
So I think this should work:
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
NSTextStorage *textStorage = [textView textStorage];
if ([[textStorage string] length] > 1) {
NSColor *color = [NSColor redColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, [NSFont ...whatever...], NSFontAttributeName, nil];
[textStorage setAttributes:attributes range:NSMakeRange(1, 1)];
}
}

Resources