0

I have a NSTextField in a view where layout is totally controlled by constraints and translatesAutoresizingMaskIntoConstraints is NO. I tried to use setStringValue to change the content like this:

[[self textfield] setStringValue:@"1\n2\n3\n"];

Then the height is changed to 4 lines which is not what I want. I need a NSTextField that can show only one line but still I can use up and down arrow keys to go into different lines. It is just like use option+enter to insert a newline in NSTextField.

I also tried to keep the height:

NSRect originalFrame = [[self textfield] frame];
[[self textfield] setStringValue:@"1\n2\n3\n"];
NSRect newFrame = [[self textfield] frame];
newFrame.size.height = originalFrame.size.height;
[[self textfield] setFrame:newFrame];

It doesn't work. I checked intrinsicContentSize and it returns (width=-1, height=73). Is there anything I can set to NSTextField so the height is of only one line like 22?

user4187988
  • 55
  • 1
  • 4

1 Answers1

1

This is happening because you have set the auto layout of textfield with respect to the view. So just fixed the height of your textfield by clicking on the height checkbox like that below :- enter image description here

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56