【问题标题】:Draw text with in a rect with given width在给定宽度的矩形中绘制文本
【发布时间】:2010-12-26 22:38:57
【问题描述】:

我正在尝试在 NSView 子类上绘制文本。我正在使用 NSTextStorage、NSLayoutManager 和 NSTextContainer。我遵循 Apple 的文档,这是我用来计算每个文本的必要高度的方法:

NSSize newSize= NSMakeSize(width, 0.0);
NSLayoutManager* layoutManager= [[textStorage layoutManagers] objectAtIndex:0];
NSTextContainer* textContainer= [[layoutManager textContainers] objectAtIndex:0];

[textContainer setContainerSize:newSize];
[textStorage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [textStorage length])];
[textContainer setLineFragmentPadding:0.0];
[layoutManager glyphRangeForTextContainer:textContainer];

return [layoutManager usedRectForTextContainer:textContainer].size.height;

唯一的问题是我不知道如何使用布局管理器绘制 NSRect。

你能帮帮我吗? 提前致谢!

【问题讨论】:

    标签: cocoa


    【解决方案1】:

    问题解决了,我是在画完之后调用这个方法的……

    最快的话题解决了^^

    【讨论】:

    • 您应该将您的问题标记为已回答,即使您自己回答了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多