Centering text from an NSAttributedString in an NSTextField [SOLVED]

Discussion in 'Mac Programming' started by Eraserhead, Mar 10, 2008.

  1. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    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?
     
  2. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    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.
     

Share This Page