【问题标题】:Aligning NSTextField and an Image对齐 NSTextField 和图像
【发布时间】:2014-03-08 18:27:53
【问题描述】:

我正在尝试对齐 NSTextField 和 NSImageView.. 我当前的代码如下。我尝试了许多不同的方法,包括子类化 NSTextFieldCell(在 SO 上找到)、弄乱文本字段的框架以及调整约束,但我就是无法得到它。不管我做什么,它看起来就像下面的截图一样..

我还发现,当我不对标签应用字体时,对齐会按我的预期工作——它与图像垂直对齐。

所以问题是,a)为什么在世界上应用字体会搞砸对齐,b)我如何解决这个问题,理想情况下,如果我在运行时更改字体,我会以一种动态的方式来适应。

- (id)initWithFrame:(NSRect)frameRect {
    self = [super initWithFrame:frameRect];
    if (self) {
        NSView *spacer1 = [[NSView alloc] init];
        [spacer1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:spacer1];

        NSView *spacer2 = [[NSView alloc] init];
        [spacer2 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:spacer2];

        NSImage *icon = [NSImage imageNamed:@"05-arrow-west"];
        NSImageView *iconView = [[NSImageView alloc] init];
        [iconView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [iconView setImage:icon];

        [self addSubview:iconView];

        NSFont *font = [NSFont fontWithName:@"HelveticaNeue-Light" size:14];
        NSTextField *label = [[NSTextField alloc] init];
        [label setTranslatesAutoresizingMaskIntoConstraints:NO];
        [label setEditable:NO];
        [label setSelectable:NO];
        [label setBezeled:NO];
        [label setDrawsBackground:NO];

        [label setFont:font];
        [label setTextColor:[NSColor lightGrayColor]];
        [label setStringValue:@"Test String"];


        [self addSubview:label];

        NSDictionary *views = NSDictionaryOfVariableBindings(iconView, label, spacer1, spacer2);

        [self addConstraints:
                [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[spacer1(>=0)][iconView]-5-[label][spacer2(==spacer1)]|"
                                                        options: 0
                                                        metrics:nil
                                                          views:views]];

        [self addConstraints:
                [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[iconView]|"
                                                        options: 0
                                                        metrics:nil
                                                          views:views]];

        [self addConstraints:
                [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|"
                                                        options: 0
                                                        metrics:nil
                                                          views:views]];

        [self setContentHuggingPriority:200 forOrientation:NSLayoutConstraintOrientationVertical];
        [iconView setContentHuggingPriority:200 forOrientation:NSLayoutConstraintOrientationVertical];
        [label setContentHuggingPriority:200 forOrientation:NSLayoutConstraintOrientationVertical];
    }

    return self;
}

【问题讨论】:

  • 你的问题是?
  • 哦,这很简单..他们为什么不排队?

标签: cocoa nstableview nstextfield nstextfieldcell nstablecellview


【解决方案1】:

这是一个非常常见的问题,与 AppKit 为某些字体计算不正确的度量有关。 Helvetica Neue 及其变体容易受到此问题的影响。自动布局取决于NSTextFieldintrinsicContentSize,它使用破碎的度量来计算适当的大小来显示文本。我知道解决此问题的唯一方法是在布局约束中使用魔术偏移来手动对齐文本。

【讨论】:

  • 嗯……糟透了……你能告诉我如何以动态方式进行魔术偏移的示例吗?我可以使用 |-5-[label(==26)]-5-| 让它对齐但它基本上破坏了自动布局——如果我改变任何其他约束,我必须弄乱这些值..
  • 我在 Autolayout 方面没有丰富的经验,但我相信 NSLayoutConstraint -constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:constant 参数可能对您有所帮助。本质上,您只想将文本字段的 Y 原点偏移一个固定数字。
【解决方案2】:

最后,它做到了:

[self addConstraint:[NSLayoutConstraint constraintWithItem:label
                                                          attribute:NSLayoutAttributeBaseline
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:iconView
                                                          attribute:NSLayoutAttributeCenterY
                                                         multiplier:1
                                                           constant:0]];

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 2012-07-02
    • 1970-01-01
    • 2010-09-20
    • 2018-01-24
    • 2020-12-19
    • 1970-01-01
    • 2020-06-04
    • 2019-05-14
    相关资源
    最近更新 更多