10

プログラムで作成しようとしていNSTextFieldます。

これを自動レイアウトで使用したいNSTextFieldので、その幅は行全体を表示するように自動的に定義されます (テキストは 1 行のみです)。

問題は、textField.intrinsicContentSize以下textField.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である理由を説明することはできませんが、属性文字列のサイズに基づいて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 に答える