NSTextField和AutoLayout:自动增长高度->以编程方式 [英] NSTextField And AutoLayout: Autogrow height -> Programmatically

查看:87
本文介绍了NSTextField和AutoLayout:自动增长高度->以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在界面构建器中创建标签并通过不适合其当前大小的代码设置字符串,则标签将垂直增长以适合其大小,太好了! 除了x& y约束,Xcode创建了一个NSContentSizeLayoutConstraint,它包含了抗拥抱性和抗压缩性.

If I create a label in interface builder and set a string through code that does not fit its current size, the label will grow vertically to fit its size, great!. Besides the x & y constraint, Xcode creates a NSContentSizeLayoutConstraint that contains hugging and compression resistance.

这是约束的日志输出

<NSContentSizeLayoutConstraint:0x6080000a11a0 V:[NSTextField:0x600000180c30(17)] Hug:750 CompressionResistance:750>

问题是,如何以编程方式执行此操作?我还没有找到办法.无法创建NSContentSizeLayoutConstraint,并且设置NSTextField的contentCompressionResistance似乎没有任何作用.

The question is, how to do this programmatically? I have not found a way yet. There is no way to create a NSContentSizeLayoutConstraint and setting the contentCompressionResistance of the NSTextField does not seem to have any effect.

这是我的代码:

 self.label = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 20)];
[self.label setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.label setBordered:YES];
[[self.label cell] setLineBreakMode:NSLineBreakByWordWrapping];
[self.window.contentView addSubview:self.label];
 self.label.stringValue = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

NSDictionary *dict = @{@"label" : self.label};
[self.label.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]"options:0 metrics:nil views:dict]];
[self.label.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label]" options:0 metrics:nil views:dict]];

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:40];
constraint.priority = 1;
[self.label addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:18];
constraint.priority = 1;
[self.label addConstraint:constraint];

[self.label setContentCompressionResistancePriority:1000 forOrientation:NSLayoutConstraintOrientationHorizontal];
[self.label setContentCompressionResistancePriority:1000 forOrientation:NSLayoutConstraintOrientationVertical];

推荐答案

设置文本字段的preferredMaxLayoutWidth.我认为您然后不需要或不需要显式的宽度约束.

Set the text field's preferredMaxLayoutWidth. I think you then don't want or need the explicit width constraint.

在我的测试中,我发现这可行:

In my testing, I find that this works:

self.label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 18)];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
[self.label.cell setLineBreakMode:NSLineBreakByWordWrapping];
self.label.stringValue = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
self.label.preferredMaxLayoutWidth = 100;
self.label.editable = NO;

[self.window.contentView addSubview:self.label];

NSDictionary* views = @{ @"label" : self.label };
[self.label.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]" options:0 metrics:nil views:views]];
[self.label.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label]" options:0 metrics:nil views:views]];

基本上,与您的主要区别是我将editable设置为NO.我还省略了宽度和高度限制,并设置了抗压强度.

Basically, the main difference from yours is that I set editable to NO. I also leave out the width and height constraints and setting the compression resistance.

这篇关于NSTextField和AutoLayout:自动增长高度-&gt;以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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