Pregunta

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!

¿Fue útil?

Solución

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.

Otros consejos

You are missing an attribute: NSParagraphStyleAttributeName, which is a NSParagraphStyle attribute. If you create a NSParagraphStyle object, you could set this property:

@property(readonly) NSTextAlignment alignment;  // readwrite for NSMutableParagraphStyle

In your case you should set it to NSTextAlignmentLeft, or NSTextAlignmentRight.

Example:

NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init];
style.alignment= NSTextAlignmentLeft;
[textAttrib setObject: style forKey: NSParagraphStyleAttributeName];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top