Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
OK I'm having problems centering some text in an NSTextField when it is sent an NSAttributedString, it seems to always left align it.

Rather than using setStringValue, I'm using setObjectValue to set it, and I assume that is was is breaking the centering. Is it possible to center the NSAttributedString itself or something?
 
I solved it.

The NSAttributedString in my case is actually an NSMutableAttributedString, so the following code works.

Code:
//center the text
NSMutableParagraphStyle *mutParaStyle=[[NSMutableParagraphStyle alloc] init];
[mutParaStyle setAlignment:NSCenterTextAlignment];
[attrStr addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle forKey:NSParagraphStyleAttributeName] range:NSMakeRange(0,[attrStr length])];
[mutParaStyle release];
//set the text.
[baseDamageTF setObjectValue:attrStr];
You could still use the mutParaStyle as an attribute for a normal NSAttributeString if required.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.