• <bdo id='8gDzN'></bdo><ul id='8gDzN'></ul>

      <i id='8gDzN'><tr id='8gDzN'><dt id='8gDzN'><q id='8gDzN'><span id='8gDzN'><b id='8gDzN'><form id='8gDzN'><ins id='8gDzN'></ins><ul id='8gDzN'></ul><sub id='8gDzN'></sub></form><legend id='8gDzN'></legend><bdo id='8gDzN'><pre id='8gDzN'><center id='8gDzN'></center></pre></bdo></b><th id='8gDzN'></th></span></q></dt></tr></i><div id='8gDzN'><tfoot id='8gDzN'></tfoot><dl id='8gDzN'><fieldset id='8gDzN'></fieldset></dl></div>
    1. <tfoot id='8gDzN'></tfoot>

      <small id='8gDzN'></small><noframes id='8gDzN'>

    2. <legend id='8gDzN'><style id='8gDzN'><dir id='8gDzN'><q id='8gDzN'></q></dir></style></legend>

      1. NSTextField 和 AutoLayout:自动增长高度 ->以编程方式

        NSTextField And AutoLayout: Autogrow height -gt; Programmatically(NSTextField 和 AutoLayout:自动增长高度 -以编程方式)
          <tbody id='2RKiH'></tbody>
              • <small id='2RKiH'></small><noframes id='2RKiH'>

              • <i id='2RKiH'><tr id='2RKiH'><dt id='2RKiH'><q id='2RKiH'><span id='2RKiH'><b id='2RKiH'><form id='2RKiH'><ins id='2RKiH'></ins><ul id='2RKiH'></ul><sub id='2RKiH'></sub></form><legend id='2RKiH'></legend><bdo id='2RKiH'><pre id='2RKiH'><center id='2RKiH'></center></pre></bdo></b><th id='2RKiH'></th></span></q></dt></tr></i><div id='2RKiH'><tfoot id='2RKiH'></tfoot><dl id='2RKiH'><fieldset id='2RKiH'></fieldset></dl></div>
              • <tfoot id='2RKiH'></tfoot>
                <legend id='2RKiH'><style id='2RKiH'><dir id='2RKiH'><q id='2RKiH'></q></dir></style></legend>

                  <bdo id='2RKiH'></bdo><ul id='2RKiH'></ul>
                • 本文介绍了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:自动增长高度 ->以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                  How do I size a UITextView to its content?(如何根据内容调整 UITextView 的大小?)
                  Can#39;t get to work CocoaPods and Yosemite(无法开始工作 CocoaPods 和 Yosemite)
                  unable to use cocoapods after updating(更新后无法使用 cocoapods)
                • <i id='RTLf3'><tr id='RTLf3'><dt id='RTLf3'><q id='RTLf3'><span id='RTLf3'><b id='RTLf3'><form id='RTLf3'><ins id='RTLf3'></ins><ul id='RTLf3'></ul><sub id='RTLf3'></sub></form><legend id='RTLf3'></legend><bdo id='RTLf3'><pre id='RTLf3'><center id='RTLf3'></center></pre></bdo></b><th id='RTLf3'></th></span></q></dt></tr></i><div id='RTLf3'><tfoot id='RTLf3'></tfoot><dl id='RTLf3'><fieldset id='RTLf3'></fieldset></dl></div>

                        <tbody id='RTLf3'></tbody>

                      • <bdo id='RTLf3'></bdo><ul id='RTLf3'></ul>

                        <small id='RTLf3'></small><noframes id='RTLf3'>

                          <tfoot id='RTLf3'></tfoot>
                        1. <legend id='RTLf3'><style id='RTLf3'><dir id='RTLf3'><q id='RTLf3'></q></dir></style></legend>