以编程方式在NSScrollView中设置NSTextView [英] Programmatically set up NSTextView inside NSScrollView

查看:567
本文介绍了以编程方式在NSScrollView中设置NSTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式在NSScrollView中设置NSTextView,并且遇到了麻烦.

I'm trying to set up an NSTextView in an NSScrollView programmatically and am having trouble.

具体来说,我无法滚动文本视图;滚动视图似乎认为这就是全部".当您尝试向下滚动以查看其余内容(不发布)时,结果如下:

Specifically, I can't scroll the text view; the scroll view seems to think "this is all there is". When you try to scroll down to see the rest of the content (without releasing), this is the result:

释放后,滚动视图会弹回,因此它不是图形故障;它只是将文本视图剪切到框架高度.

As soon as you release, the scroll view bounces back, so it's not a graphical glitch; it simply clips the text view at the frame height.

我尝试使用 Apple进行试验"Text in ScrollView"文档,并且我已经尝试过setVerticallyResizable:等.

I've tried experimenting with the Apple 'Text In ScrollView' docs and I've experimented with setVerticallyResizable: etc.

代码:

- (void)configureValueTextView
{
    NSScrollView *vfContainer = [[NSScrollView alloc] initWithFrame:(NSRect){0,0,50,50}];
    vfContainer.hasVerticalScroller = YES;
    vfContainer.hasHorizontalScroller = NO;
    vfContainer.translatesAutoresizingMaskIntoConstraints = NO;
    _valueFieldContainer = vfContainer;
    vfContainer.borderType = NSLineBorder;

    NSTextView *valueField = _valueField = [[NSTextView alloc] initWithFrame:(NSRect){0,0,vfContainer.contentSize}];
    valueField.delegate = self;
    valueField.minSize = (NSSize){0,0};
    valueField.maxSize = (NSSize){FLT_MAX, FLT_MAX};
    [valueField setVerticallyResizable:YES];
    [valueField setHorizontallyResizable:YES];

    valueField.translatesAutoresizingMaskIntoConstraints = NO;
    valueField.string = _value ? _value : @"";
    valueField.editable = YES;
    valueField.textContainer.containerSize = (NSSize) { vfContainer.contentSize.width, FLT_MAX };
    valueField.textContainer.heightTracksTextView = NO;

    vfContainer.documentView = valueField;
    [vfContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[_valueField]-(0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueField)]];
    [vfContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[_valueField]-(0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueField)]];
}

稍后将帧调整为正确的值,

The frames are later adjusted to their right values,

[_valueFieldContainer setFrame:(NSRect){kKEFormFieldViewKeyWidth + 10, 0, valueFieldWidth, h}];
[_valueField setFrame:(NSRect){0, 0, valueFieldWidth, h}];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[_valueFieldContainer]-(5)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueFieldContainer)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[_keyField]-(10)-[_valueFieldContainer]-(0)-|" options:0 metrics:nil views:@{@"_keyField": _keyField, @"_valueFieldContainer": _valueFieldContainer}]];

就是这样.欢迎提示!

推荐答案

您已经设置了垂直约束,使文本视图与其父视图(滚动视图的剪辑视图)的高度匹配.我认为这就是问题所在.如果仅将其完全删除,则在垂直方向上将有完全的歧义,并且文本视图可以在任何地方结束.我认为您只需将其顶部边缘固定在超级视图的顶部即可-即@"V:|[_valueField]".如果文本视图提供了固有的高度,那将起作用,但是我不确定是否可以.为文本视图的高度设置一个约束,使其与文本布局管理器将产生的约束不平凡.

You've set a vertical constraint making the text view match the height of its superview (the clip view of the scroll view). I think that's what going wrong. If you just totally remove that, you've got complete ambiguity in the vertical direction and the text view can end up anywhere. I think you can get away with just pinning its top edge to the superview's top – i.e. @"V:|[_valueField]". That will work if the text view provides an intrinsic height, but I'm not sure it does. Setting up a constraint for the text view's height such that it matches what the text layout manager will produce is non-trivial.

鉴于您实际上并不需要布局中的任何特殊内容,您可以使用旧样式的自动调整大小蒙版来代替自动布局.

Given that you don't really need anything special from the layout, you can just use the old-style autoresizing mask instead of auto layout.

这篇关于以编程方式在NSScrollView中设置NSTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆