-[NSTextField intrinsicContentSize] always has undefined width

给你一囗甜甜゛ 提交于 2020-01-21 11:25:28

问题


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"

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

I've tried calling invalidateIntrinsicContentSize but to no avail…

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.


回答1:


OK, finally figured it out…

[_textfield setEditable:NO]

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.




回答2:


If you do need an editable textfield, make sure the NSTextField has a width constraint set to the minimum width the view should be.



来源:https://stackoverflow.com/questions/20809622/nstextfield-intrinsiccontentsize-always-has-undefined-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!