objective-cmacosnsviewnsfont

String Alignment in NSFont / NSView


He, when I draw Text into my view like this:

NSMutableDictionary *textAttrib = [[NSMutableDictionary alloc] init];
[textAttrib setObject:[NSFont fontWithName:@"Helvetica Light" size:15] forKey:NSFontAttributeName];
[textAttrib setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
[mouseString drawAtPoint:textPoint withAttributes:textAttrib];

how can I change the alignment of the text? I just need simple left/right alignment and can't find any info about that, only in textfields. Thanks!


Solution

  • There is no such concept of alignment with the [NSString drawAtPoint:withAttributes:] method, and this makes sense if you think about it. Alignment is concept of how a string appears relative to the content of a control (classically an NSTextField).

    You could, however, try [NSString drawInRect:withAttributes:] where that concept does exist. See this SO question.

    UPDATE: This question isn't complete without the (now deleted) answer given by @justin, which shows how to set the text alignment in an NSAttributedString. Both are answers are correct, but neither is complete.