Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
936 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...