【问题标题】:NSView resizingNSView 调整大小
【发布时间】:2011-04-09 07:26:32
【问题描述】:

我有一个带有 NSTextField 的 NSView。问题是,每次在显示视图之前, NSTextField 的大小都会改变,我希望 NSView 的大小随之改变。因此,如果文本字段长 2 行,则 nsview 会很小(仅围绕文本字段)但是当它有 10 行长时,我不希望视图的大小切断文本字段,我希望视图随之增长。

我该怎么做?谢谢。

【问题讨论】:

  • 我认为您将需要添加更多信息。例如,文本是如何输入的,是否包含新行,您希望更改宽度、高度还是两者都更改。我的想法是你需要计算字符的长度并以此调整视图的矩形。即每行 20 个字符长,每 20 个字符增加大小

标签: objective-c cocoa nsview nstextfield


【解决方案1】:

这是我在我的表格视图中使用的,但它应该同样适用于您的情况:

    float textLabelWidth = 190;

    NSString *Text = [[deals objectAtIndex:[indexPath section]] objectForKey:@"title"];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:13.0];
    CGSize constraintSize = CGSizeMake(textLabelWidth, MAXFLOAT);
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20; // <--- this is what your NSView's height should be, just edit the textLabelWidth and padding (in this case 20) to whatever you desire.

希望有帮助!

【讨论】:

    【解决方案2】:

    您需要设置“contentHugging 和 contentCompression”优先级。

    简而言之:

    contentHugging: sets the preference where a control resists being made *larger* than it's intrinsic size
    
    contentCompression: sets the preference where a control resists being made *smaller* than it's intrinsic size
    

    苹果在这里有一个参考:

    https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html

    具体向下滚动到Setting Content-Hugging and Compression-Resistance Priorities 部分。

    这就是处理文本字段等...剪辑(即,当文本字段具有“某些 testValue”时变为“某些 testV...”等...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      相关资源
      最近更新 更多