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

问题描述

我在这里拉毛发。我有一个对的NSTextField有调整,这是一个stringValue的一个intrinsicContentSize厦门国际银行创建的。

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总是(宽度= -1,身高= 16)

我已经打过电话invalidateIntrinsicContentSize但无济于事...

I've tried calling invalidateIntrinsicContentSize but to no avail…

怎样才能获得的NSTextField的intrinsicContentSize工作?我必须做的东西比从厦门国际银行的不同,我只是无法弄清楚。

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.

推荐答案

OK,终于搞明白了...

OK, finally figured it out…

[_文本框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]总是有不确定的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 00:41