【发布时间】:2011-05-02 14:48:00
【问题描述】:
我正在尝试在中心对齐的可可 NSView 中绘制一个带有新行 (\n) 的字符串。例如,如果我的字符串是:
NSString * str = @"this is a long line \n and \n this is also a long line";
我希望这看起来有点像:
this is a long line
and
this is also a long line
这是我在 NSView drawRect 方法中的代码:
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];
它仍然以左对齐方式绘制文本。这段代码有什么问题?
【问题讨论】: