I am creating cocoa app in which i created NSTextField
programmatically like this,
NSView *superView = [[NSView alloc] initWithFrame:NSMakeRec
For adjusting the text field to string size you have to calculate the text size with NSString
's sizeWithFont: constrainedToSize: lineBreakMode:
before and than adjust the text field's size with setting the frame.
For vertical alignment look at this question (and the answers).
override ViewWillDraw and position your NSTextField in the center of its parent view at this point. This works with live resizing as well. You can also adjust font size at this point to find a font which fits the new size.
What you want is possible, but you'll have to make a subclass of NSTextFieldCell, and then use that subclass in your NSTextField. The key methods you want override are drawingRectForBounds:
, selectWithFrame:
, and editWithFrame:
Here is a blog post from the fantastic Daniel Jalkut about this, he even includes a downloadable version ready to go. The post is fairly old but it should still work fine.