PDA

View Full Version : Centering text from an NSAttributedString in an NSTextField [SOLVED]




Eraserhead
Mar 10, 2008, 05:45 PM
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?



Eraserhead
Mar 10, 2008, 06:02 PM
I solved it.

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


//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.