3

NSPopupButtonがあり、選択したタイトルに合わせてサイズを変更したいのですが。

[NSPopupButton sizeToFit]は、ポップアップが現在選択されているものではなく最大のタイトルアイテムにサイズ変更されているため、私のニーズに合いません。

私は成功せずにいくつかの方法で試しましたが、近いほど

#define ARROW_WIDTH 20
NSDictionary *displayAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[popup font], NSFontAttributeName, nil];
NSSize titleSize = [popup.titleOfSelectedItem sizeWithAttributes:displayAttributes] + ARROW_WIDTH;

しかし、定数値ARROW_WIDTHは、非常にダーティでエラーが発生しやすいソリューションです。

ステータスバーのTextWranglerエンコーディングコンボは必要なように機能します

4

2 に答える 2

6

テキストフィールドでこれらの問題を処理した方法は、ビュー階層に追加しないテキストフィールドでサイズ変更を試すことです。再利用する予定のないオブジェクトに対してsizeToFitを呼び出し、それを使用して、実際のコントロールが必要な操作に適合するために必要な幅を計算します。

したがって、擬似コードでは、これを行います(これはリークするため、非ARCプロジェクトにARC、YMMVを使用していると仮定します)。

NSArray *popupTitle = [NSArray arrayWithObject: title];
NSPopUpButton *invisiblePopup = [[NSPopUpButton alloc] initWithFrame: CGRectZero pullsDown: YES];
// Note that you may have to set the pullsDown bool to whatever it is with your actual popup button.
[invisiblePopup addItemWithTitle: @"selected title here"];
[invisiblePopup sizeToFit];
CGRect requiredFrame = [invisiblePopup frame];
self.actualPopup.frame = requiredFrame;
于 2012-12-05T09:47:47.927 に答える
1

intrinsicContentSizeのサブクラスにautolayout オーバーライド メソッドがあるプロジェクトの場合NSPopUpButton

class NNPopUpButton: NSPopUpButton {
    override var intrinsicContentSize: NSSize {
        let fakePopUpButton = NSPopUpButton(frame: NSZeroRect, pullsDown: false)
        fakePopUpButton.addItem(withTitle: title)
        fakePopUpButton.sizeToFit()
        var requiredFrame = fakePopUpButton.frame
        requiredFrame.size.width -= 35 // reserved space for key equivalent
        return requiredFrame.size
    }
}
于 2018-09-16T06:45:20.733 に答える