-[NSTextField intrinsicContentSize] 始终具有未定义的宽度 [英] -[NSTextField intrinsicContentSize] always has undefined width

查看:22
本文介绍了-[NSTextField intrinsicContentSize] 始终具有未定义的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里扯头发.我在 xib 上创建了一个 NSTextField,它的 intrinsicContentSize 调整为它的 stringValue.

I'm pulling hairs here. I have an NSTextField created on a xib that has an intrinsicContentSize adjusted to it's stringValue.

如果我像这样以编程方式创建一个文本字段:

If I create a textField programmatically like so:

    _textfield = [[NSTextField alloc] initWithFrame:CGRectZero];
    _textfield.translatesAutoresizingMaskIntoConstraints = NO;
    _textfield.alignment = NSCenterTextAlignment;
    _textfield.drawsBackground = NO;
    [_textfield setBordered:NO];
    _textfiled.stringValue = @"Test"

intrinsicContentSize 总是 (width=-1, height=16)

It's intrinsicContentSize is always (width=-1, height=16)

我尝试调用 invalidateIntrinsicContentSize 但无济于事……

I've tried calling invalidateIntrinsicContentSize but to no avail…

如何让 NSTextField 的 intrinsicContentSize 工作?我一定是在做一些不同于 xib 的事情,我就是不知道是什么.

How does one get NSTextField's intrinsicContentSize to work?? I must be doing something different than the one from the xib, I just can't figure out what.

推荐答案

好的,终于想通了……

[_textfield setEditable:NO]

就是这样.我猜想对于可编辑的文本字段,必须对文本字段的宽度有明确的约束.哪一种是有意义的,想象一下编辑一个文本字段,它会随着每次击键而不断水平增长……这不是一个理想的 UI.

That's it. I guess that with an editable textfield one must have an explicit constraint for the textfield width. Which kind of makes sense, imagine editing a textfield and it would constantly grow horizontally with every keystroke... not an ideal UI.

这篇关于-[NSTextField intrinsicContentSize] 始终具有未定义的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆