NSButton setAlignment does not work - cocoa

I used
[button setAlignment:NSCenterTextAlignment];
to make the text display at the center of the button.
It worked.
But if I set button title attribute before the code, button 'setAlignment' will not work
- (void)setButtonTitle:(NSButton*)button fontName:(NSString*)fontName fontSize:(CGFloat)fontSize fontColor:(NSColor*)fontColor;
{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:fontName size:fontSize]
forKey:NSFontAttributeName]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:fontColor
range:NSMakeRange(0, [[button title] length] )];
[button setAttributedTitle: attributedString];
[button setAlignment:NSCenterTextAlignment];//button title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
}
title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
Welcome any comment

Since you’re using an attributed string for the button title, the attributes in that string are responsible for setting the alignment.
To centre that attributed string, add an NSParagraphStyleAttributeName attribute with a centred alignment value:
NSMutableParagraphStyle *centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[centredStyle setAlignment:NSCenterTextAlignment];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:fontName size:fontSize],
NSFontAttributeName,
fontColor,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:attrs];
[button setAttributedTitle: attributedString];
In the code above, I’ve created a single attrs dictionary holding all the attributes for the attributed string. From your code, it looks like the font colour should be applied to the whole string anyway.

Related

NSMutableAttributedString set font size

Every resource I keep coming across for this is for iOS. I can't seem to get this working properly for Mac OSX. Any idea how to get this to set the font size as well? It currently sets the color and center alignment properly. Thanks
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:#"Library"];
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[#"Library" length] ) ];
[libTitle setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [#"Library" length])];
[libTitle addAttribute:NSFontSizeAttribute value:[NSFont systemFontOfSize:18.0] range:NSMakeRange(0, [#"Library" length])];
[self.libbtn setAttributedTitle:libTitle];
Try setting your font attributes from the get go, in the init method:
NSFont *systemFont = [NSFont systemFontOfSize:18.0f];
NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil];
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:#"Library" attributes:fontAttributes];
NSRange rangeOfTitle = NSMakeRange(0,[libTitle length]);
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:rangeOfTitle];
[libTitle setAlignment:NSCenterTextAlignment range:rangeOfTitle];
[self.libbtn setAttributedTitle:libTitle];

NSButton remove text shadow

I use standard NSButton with custom background. With black title color there is white shadow - how can I remove it?
Image:
http://i.piccy.info/i7/f4ae52b56aad922f0129e4b6bd8688da/4-57-36/57765457/Snymok_ekrana_2013_03_19_v_04_13_58.png
Solved!
NSAttributedString documentation says that default value of NSShadowAttributeName is nil, but in this case the button title is drawn with white shadow. Transparent shadow solved problem:
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor colorWithDeviceWhite:1.0 alpha:0.0]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
shadow, NSShadowAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:_customTitle attributes:attrsDictionary];
[mybutton setAttributedTitle:attrString];

Can set either alignment or font, but not both, for NSTextField

I have a panel nib with an outlet for one of its textfields, which is set in the nib to have centered alignment. When I display the panel, I would like this textfield to be bolded.
Since NSTextField is a subclass of NSControl, it can use the setAttributedStringValue method and take an attributed string. So I incorporated a bold font like this:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObject:fontBolded forKey:NSFontAttributeName];
NSString *sHelloUser = NSLocalizedString(#"Hello User", #"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];
[attrsHelloUser release];
The bolding shows up OK, but the field is now left-aligned.
I tried adding a setAlignment, but it had no effect:
[self.fooController.tfPanelCenteredField setAlignment:NSCenterTextAlignment];
So I tried adding a centered parapraph style to the attributed string’s attributes:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:paragStyle, NSParagraphStyleAttributeName, fontBolded, NSFontNameAttribute, nil];
NSString *sHelloUser = NSLocalizedString(#"Hello User", #"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];
[attrsHelloUser release];
[paragStyle release];
Now the textfield is centered again, but the bolding is gone. It’s as though the attributed string can accept one and only one attribute setting. Am I missing something simple?
You have a typo in your code. NSFontNameAttribute should be NSFontAttributeName.
So your attributes dictionary is:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:
fontBolded, NSFontAttributeName,
paragStyle, NSParagraphStyleAttributeName,
nil];

NSString *text to NSString *icon?

I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image.
I want it to be like other apps where it shows icon.png when not clicked and icon-active.png when clicked.
The current code is this:
- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
[[NSColor selectedMenuItemColor] set];
NSRectFill(rect);
}
// Draw some text, just to show how it's done.
NSString *text = #"3"; // whatever you want
NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
textColor = [NSColor selectedMenuItemTextColor];
}
NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
msgFont, NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
paraStyle, NSParagraphStyleAttributeName,
nil];
[paraStyle release];
NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;
[text drawInRect:msgRect withAttributes:msgAttrs];
}
Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501.
Thanks!
Use an NSImage and draw it where desired. For example:
NSString *name = clicked? #"icon-active" : #"icon";
NSImage *image = [NSImage imageNamed:name];
NSPoint p = [self bounds].origin;
[image drawAtPoint:p fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
If this is for a status item and you just want an icon with no programmatic drawing, drop the view and set the status item's image and alternateImage. The former is what the status item uses normally; the status item switches to the alternate image (if it has one) when the user opens its menu.

Showing image before text in NSButtonCell in NSMatrix

I am displaying buttons in NSMatrix.
My requirement is:
to change color of button title and
place an image at beginning of title, when certain condition is satisfied.
To do so, I used following code:
// setting attributed text
NSAttributedString *selectedCellAttribute;
NSFont *selectedCellFont = [NSFont fontWithName:#"Lucida Grande" size:11];
NSColor *selectedCellColor = [NSColor redColor];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSCenterTextAlignment];
// setting image
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
NSCell *cell = [imageAttachment attachmentCell];
[cell setImage:[NSImage imageNamed:#"caution_small.png"]];
NSDictionary *selectedCellDictionary = [NSDictionary dictionaryWithObjectsAndKeys:imageAttachment,NSAttachmentAttributeName,selectedCellFont,NSFontAttributeName,selectedCellColor,NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil];
// recognizing cell
NSButtonCell *associatedCell = [associatesMatrix cellAtRow:0 column:2];
selectedCellAttribute = [[NSAttributedString alloc] initWithString:[associatedCell title] attributes:selectedCellDictionary];
[associatedCell setAttributedTitle:selectedCellAttribute];
Although above code is showing change in color of title, it is showing no image placed in beginning of title :(
Can anyone suggest me where I may be wrong or some other method to implement my requirements?
EDIT:
At line:
NSCell *cell = [imageAttachment attachmentCell];
it is giving this warning when compiled:
type 'id <NSTextAttachmentCell>' does not conform to 'NSCopying" protocol.
Thanks,
Miraaj
You've set the attachment for the entire string. What you need to do is prefix the string with NSAttachmentCharacter, and set the attachment only for that section of the string.
You may want to put a space between the NSAttachmentCharacter and your actual text. Only the NSAttachmentCharacter should have the attachment attribute.

Resources