NSLayoutManager文本新行似乎从矩形的底部开始向上

时间:2011-06-19 16:59:39

标签: objective-c cocoa macos text nslayoutmanager

m_LayoutManager = [[NSLayoutManager alloc] init];
m_TextContainer = [[NSTextContainer alloc] init];
m_TextStorage = [[NSTextStorage alloc] initWithString: @"Here's to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes, the ones who see things differently."];  
[m_LayoutManager addTextContainer: m_TextContainer];
[m_TextStorage addLayoutManager: m_LayoutManager];
[m_TextContainer release];
[m_LayoutManager release];
[m_TextStorage replaceCharactersInRange:NSMakeRange(0, [m_TextStorage length]) withString: XMajor];
[m_TextContainer setContainerSize: NSMakeSize(75,50)];

NSRange glyphRange = [m_LayoutManager glyphRangeForTextContainer: m_TextContainer];
NSRect usedRect = [m_LayoutManager usedRectForTextContainer: m_TextContainer];

[[NSColor brownColor] setFill];
//NSRectFill( NSMakeRect(j - usedRect.size.width/2,y0 - 50 - 5,50,50));

[m_LayoutManager drawGlyphsForGlyphRange:glyphRange 
                                 atPoint: NSMakePoint(j - usedRect.size.width/2,y0 - 50 - 5)];  

我最终看到的是从矩形底部开始向上的文本。当我在视图中绘制时,它看起来像这样:

ferently.
the ones who see things dif
nd pegs in the square holes, 
the troublemakers, the rou
 the misfits, the rebels,
Here's to the crazy ones,

我希望文本从顶部开始然后向下,我也希望文本自动换行,现在它将在新行开始一个单词的中间。我了解了NSLayoutManagerNSParagraphStyleNSTypeSetter,但我无法弄清楚如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

覆盖isFlipped以在NSView的子类中返回YES。