NSLayoutManager Text 新行似乎从矩形底部开始向上

发布于 2024-11-16 07:24:10 字数 1433 浏览 3 评论 0原文

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 的内容,但我不知道如何解决此问题。

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)];  

What I end up seeing is the text starting from the bottom of the rectangle and going up. It looks like this when I draw it in view:

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,

I want the text to start from top and go down and I also want the text to word wrap, right now it will start the middle of a word on a new line. I read about NSLayoutManager, NSParagraphStyle, and NSTypeSetter, but I cannot figure out how to fix this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-11-23 07:24:10

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

Override isFlipped to return YES in your subclass of NSView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文