Mirror318
Mirror318

Reputation: 12693

How to resize NSTextField dynamically?

I want an NSTextField to get bigger as the user types, and smaller as backspace is pressed etc.

The only thing I've found is [textField sizeToFit] but that doesn't run dynamically, it's just a one off resizing. Is there a property or something I need to set?

p.s. I only need it to resize horizontally (single line text).

Upvotes: 0

Views: 541

Answers (2)

pseudosudo
pseudosudo

Reputation: 6600

What Richard said, except you need to know by how much to resize your textfield. To get this value, use the the layout manager of the NSTextField and call something like boundingRectForGlyphRange:inTextContainer: on it with a range of all glyphs. This should tell you the dimensions of the rectangle bounding all of your text. Take these dimensions and set them to the width and height of your NSTextField content.

Upvotes: 1

Richard Stahl
Richard Stahl

Reputation: 364

Set a delegate on the NSTextField which implements

- (void)controlTextDidChange:(NSNotification *)aNotification

Every time the delegate gets called resize the control per your idea above.

Upvotes: 0

Related Questions