【发布时间】:2013-11-04 21:52:29
【问题描述】:
我在NSView 中有一个NSTextView(NSPopover 正在使用它,不知道这是否相关),我正在尝试以编程方式自动调整大小(参见标题)。
我一直在为很多事情苦苦挣扎,即:
- 查看
NSLayoutManager和usedRectForTextContainer给我的尺寸值异常 (usedRectForTextContainer : {{0, 0}, {0.001, 28}}) - 修改
NSScrollView frame、[NSScrollView contentView]、[NSScrollView documentView] - 摆脱自动布局
我可以调整 scollview 和 Popover 的大小,但我无法获得 NSTextView 中文本的实际高度。
我们将不胜感激。
-(void)resize
{
//Get clipView and scrollView
NSClipView* clip = [popoverTextView superview];
NSScrollView* scroll = [clip superview];
//First, have an extra long contentSize and ScrollView to test everything else
[popover setContentSize:NSMakeSize([popover contentSize].width, 200)];
[scroll setFrame:(NSRect){
[scroll frame].origin.x,[scroll frame].origin.y,
[scroll frame].size.width,155
}];
NSLayoutManager* layout = [popoverTextView layoutManager];
NSTextContainer* container = [popoverTextView textContainer];
NSRect myRect = [layout usedRectForTextContainer:container]; //Broken
//Now what ?
}
【问题讨论】:
-
这种情况是否仅在第一次调用 resize 时发生?我问是因为 usedRectForTextContainer: 在返回矩形之前不做任何布局。这是在小牛队吗?
-
是的,它在 Mavericks 上,不,它变化很大(第一次调用,给出一个 OK 的结果,然后它的高度缩小到 0.001)。如果您在 Github 上,如果您想尝试 fork 并使用它,该项目是 github.com/bertrand-caron/BCFirstLaunchTutorial。
-
很愿意。谢谢。
-
有趣的项目
标签: cocoa nstextview nsscrollview