10

我正在尝试以NSTextField编程方式创建一个。

我想将此NSTextField与自动布局一起使用,因此将自动定义其宽度以显示整行(只有一行文本)。

问题是textField.intrinsicContentSizetextField.fittingSize的水平坐标都有 -1 和 0 值,因为下面代码的输出是: textField.intrinsicContentSize={-1, 21} textField.fittingSize={0, 21}

编码:

NSTextField* textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 10, 24)];    
NSString* str = @"One line of text here.";
[textField setStringValue: str];
[textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[textField invalidateIntrinsicContentSize];

NSLog(@"textField.intrinsicContentSize=%@", NSStringFromSize(textField.intrinsicContentSize));
NSLog(@"textField.fittingSize=%@", NSStringFromSize(textField.fittingSize));

这使得文本字段具有由自动布局分配的零宽度。

fittingSize我应该怎么做才能为文本字段的和属性获取有意义的值,intrinsicContentSize以便它们反映文本字段的内容?

4

3 回答 3

12

另一种不使用计算文本字段最佳宽度的方法fittingSize是使用以下代码(替换 John Sauer 的 sizeTextFieldWidthToFit 方法):

- (void) sizeTextFieldWidthToFit {
    [textField sizeToFit];
    CGFloat newWidth = NSWidth(textField.frame);
    textFieldWidthConstraint.constant = newWidth;
}

您可以将 textFieldWidthConstraint 的优先级设置为低于另一个不等式的优先级,将表示您的要求的最小大小限制为 25。

于 2013-02-07T20:04:55.763 回答
3

我无法解释为什么 NSTextField 的 intrinsicContentSize 的宽度为 -1,但我能够根据其属性字符串的大小来计算一个。以下是我创建 NSTextField 的方式:

// textField is an instance variable.
textField = [NSTextField new] ;
textField.translatesAutoresizingMaskIntoConstraints = NO ;
[view addSubview:textField] ;

NSDictionary* viewsDict = NSDictionaryOfVariableBindings(view, textField) ;
// textFieldWidthConstraint is an instance variable.
textFieldWidthConstraint  = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[textField(==0)]"  options:0  metrics:nil  views:viewsDict] [0] ;
[view addConstraint:textFieldWidthConstraint] ;
NSNumber* intrinsicHeight = @( textField.intrinsicContentSize.height ) ;
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textField(==intrinsicHeight)]"  options:0  metrics:NSDictionaryOfVariableBindings(intrinsicHeight)  views:viewsDict]] ;

// Position textField however you like.
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textField]"  options:0  metrics:nil  views:viewsDict]] ;
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField]"  options:0  metrics:nil  views:viewsDict]] ;

[self sizeTextFieldWidthToFit] ;
// set textField's delegate so that  sizeTextFieldWidthToFit  is called when textField's text changes.
textField.delegate = self ;

以下是在其文本更改时调整其大小的方法:

- (void) controlTextDidChange:(NSNotification*)aNotification {
    if ( aNotification.object == textField )
        [self sizeTextFieldWidthToFit] ;
}

- (void) sizeTextFieldWidthToFit {
    // I'd like the width to always be >= 25.
    CGFloat newWidth = 25 ;
    if ( ! [textField.stringValue isEqualToString:@""] ) {
        NSDictionary* attributes = [textField.attributedStringValue  attributesAtIndex:0  effectiveRange:nil] ;
        NSSize size = [textField.stringValue sizeWithAttributes:attributes] ;
        newWidth =  MAX(newWidth, size.width + 10) ;
    }
    textFieldWidthConstraint.constant  = newWidth  ;
}
于 2013-02-07T18:58:43.090 回答
0

我在这里添加这个是因为这些答案很旧,而且我找不到NSTextField在单个答案中使用自动布局和 NSConstraints 自动包装和播放良好所需的一切。

通过这些设置,文本将:

  • 保持在约束设置的水平边界内。
  • 在单词边界上自动换行。
  • 根据需要自动调整其高度。
  • 按下下面的任何东西(如果附加约束)。

这是使它正常工作的代码:

NSTextField *instructions = [NSTextField labelWithString:@"Some very long text..."];
instructions.translatesAutoresizingMaskIntoConstraints = NO;
instructions.autoresizesSubviews = YES;
instructions.usesSingleLineMode = NO;
instructions.lineBreakMode = NSLineBreakByWordWrapping;
[instructions cell].wraps = YES;
[instructions cell].scrollable = NO;
instructions.maximumNumberOfLines = 10;
instructions.preferredMaxLayoutWidth = 400.0;
[self addSubview:iCloudInstructions];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[instructions]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(instructions)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[instructions]-(20)-[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(instructions, instructions)]];
于 2018-05-12T13:58:49.050 回答