Reputation: 6333
I have instantiated the NSTextView in the code. I would like to set the line limit to 1 line in a property. I would rather not have to use a delegate.
Upvotes: 1
Views: 501
Reputation: 401
I also needed to solve this, so I created a single line NSTextField and then examined all the settings of its NSTextView field editor and the associated NSTextContainer.
By using similar settings, I created an NSTextView that is limited to one line and behaves exactly like NSTextField for left, center and right aligned text.
NSRect superViewFrame = NSMakeRect(...) // set coordinates of frame in superview coordinates
// create the scroll view
NSRect scrollFrame = NSMakeRect(0, 0, superviewFrame.width, superViewFrame.height);
NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame: scrollFrame];
[scrollview setBorderType:NSNoBorder];
[scrollview setHasHorizontalScroller:NO];
[scrollview setHasVerticalScroller:NO];
// create the NSTextView
NSTextView* view = [[NSTextView alloc] initWithFrame:superviewFrame];
[view setMinSize:superviewFrame.size]; // this is critical; it cannot be (0,0)!
[view setMaxSize:NSMakeSize(1000000, 1000000)]; // NSTextField field editor Values = 40000, 40000
[view setAlignment:align];
[view setHorizontallyResizable:YES];
[view setVerticallyResizable:YES];
[[view textContainer] setContainerSize:NSMakeSize(1000000, 20000000)]; // NSTextField field editor Values = 40000, 10000000
[[view textContainer] setWidthTracksTextView:NO];
[[view textContainer] setHeightTracksTextView:NO];
// install view in scroll view
[scrollview setDocumentView:view];
Once this is complete, install the scroll view into its superview.
Tested on Mojave (10.14.6) and Monterey (12.6.5)
Upvotes: 0
Reputation: 1083
You could just use an NSTextField
instead of an NSTextView
, they are easier to use as well
Upvotes: 2