Objective C视觉自动布局NSTextField动态高度 [英] Objective C Visual Autolayout NSTextField dynamic height

查看:214
本文介绍了Objective C视觉自动布局NSTextField动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用视觉自动布局创建动态尺寸NSView.我正在尝试实现如下图所示的内容.

I am trying to create a dynamic size NSView using visual auto layout. I am trying to achieve something like the following diagram.

我添加了以下限制条件来实现这一目标.

I added following constraints to achieve this.

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-15-[_iconImageView(39)]-12-[_textView(239)]-15-|"
                                                             options:NSLayoutFormatAlignAllTop metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[_textView]-4-[_mainButton]-5-|"
                                                             options: NSLayoutFormatAlignAllLeft metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_mainButton]-15-[_secondaryButton]"
                                                             options:NSLayoutFormatAlignAllTop metrics:nil views:views]];

但这会创建以下布局.

据我了解,以下VFL将从顶部布局_textView 15px,然后从_textView底部布局_mainButton 4px.但是实际上_mainbottom是从_textView顶部开始4px后的布局.我在这里想念东西吗?

As per my understanding, the following VFL will layout _textView 15px from top and then layout _mainButton after 4px from _textView bottom. But in actual _mainbottom is layout after 4px from top of _textView. Am i missing something here?

@" V:| -15-[_ textView] -4-[_ mainButton] -5- |"

@"V:|-15-[_textView]-4-[_mainButton]-5-|"

更新


我将NSTextView替换为NSTextField.但是现在的问题是,NSTextField的增长不会超过单行的高度,而NSTextField是多行的.布局和设置视图的完整代码如下.

Update


I replaced NSTextView with NSTextField. But now the problem is, NSTextField does not grow more than a single line height, but NSTextField is multi-line. Complete code for layouting and setting up views is as follow.

-(void)setupView {
    _textField = [[NSTextField alloc] initWithFrame:NSZeroRect];
    [[_textField cell] setWraps:YES];
    [_textField setLineBreakMode:NSLineBreakByWordWrapping];
    [[_textField cell] setTitle:@"This is a _textView, This will contain dynamic resizing text."];

    [_textField setSelectable:NO];
    [_textField setEditable:NO];
    [_textField setDelegate:self];
    [_textField setBordered:NO];
    [_textField sizeToFit];
    [_textField setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:_textField];
    
    _iconImageView = [[NSImageView alloc] initWithFrame:NSZeroRect];
    [_iconImageView setImage:[NSImage imageNamed:@"notify-warning-icon"]];
    [_iconImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:_iconImageView];
    
    _mainButton = [[NSButton alloc] init];
    _mainButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_mainButton setTitle:@"_mainButton"];
    [self addSubview:_mainButton];
    
    _secondaryButton = [[NSButton alloc] init];
    _secondaryButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_secondaryButton setTitle:@"_secondaryButton"];
    [self addSubview:_secondaryButton];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(_textField,_iconImageView,_mainButton,_secondaryButton);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-15-[_iconImageView(39)]-12-[_textField(239)]-15-|"
                                                                 options:NSLayoutFormatAlignAllTop metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[_textField]-4-[_mainButton]-5-|"
                                                                 options: NSLayoutFormatAlignAllLeft metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_mainButton]-15-[_secondaryButton]"
                                                                 options:NSLayoutFormatAlignAllTop metrics:nil views:views]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:0.f constant:320.f]];
}

但是如果它改变了

@"V:|-15-[_textField]-4-[_mainButton]-5-|"

与此

@"V:|-15-[_textField(>=40)]-4-[_mainButton]-5-|"

我得到以下输出

但是问题是文本字段的内容是动态的,并且可以在运行时更改,它可能是2行,3行等.所以我如何向NSTextField添加一些高度约束,该约束将根据其内容更改NSTextField高度

But the problem is textfield content is dynamic and could change at runtime, it could be 2 lines, 3 lines etc. So how could i add some height constraint to NSTextField that will change NSTextField height depending on its content.

推荐答案

您可以使用文本字段而不是文本视图来设置其preferredMaxLayoutWidth属性.

You can use a text field rather than a text view and set its preferredMaxLayoutWidth property.

默认情况下,如果preferredMaxLayoutWidth为0,则文本字段将计算其固有大小,就像其内容排列在一条长行中一样(或至少没有任何最大宽度).即使您应用限制其实际宽度的约束,该约束也不会改变其固有高度,因此,通常它的高度不足以包含包装后的文本.

By default, if preferredMaxLayoutWidth is 0, a text field will compute its intrinsic size as though its content were laid out in one long line (or, at least, without any maximum width). Even if you apply a constraint that limits its actual width, that doesn't change its intrinsic height and therefore it typically won't be tall enough to contain the text as wrapped.

如果设置preferredMaxLayoutWidth,则文本字段将基于包裹到该宽度的文本来计算其固有大小.这包括使其内在高度足够高以适合放置.

If you set preferredMaxLayoutWidth, then the text field will compute its intrinsic size based on the text as wrapped to that width. That includes making its intrinsic height tall enough to fit.

这篇关于Objective C视觉自动布局NSTextField动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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