I am trying to draw a line with new lines (\ n) in cocoa NSView centered. For example, if my line is:
NSString * str = @"this is a long line \n and \n this is also a long line";
I would like it to look like:
this is a long line
and
this is also a long line
Here is my code inside the NSView drawRect method:
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setAlignment:NSCenterTextAlignment];
NSDictionary * attributes = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
NSString * mystr = @"this is a long line \n and \n this is also a long line";
[mystr drawAtPoint:NSMakePoint(20, 20) withAttributes:attributes];
He still draws the text aligned to the left. What is wrong with this code?
source
share