I work with Xcode5.

I would like the width of my NSTextField to adapt to its content.

If possible, I would like to do this in InterfaceBuilder and if possible, without subclassing.

EDIT : I am working with autolayout ON !! (which seems to prevent me from changing the width of my NSTextField)

有帮助吗?

解决方案 2

In the documentation for a NSTextField there is nothing that shows that a NSTextField can resize depending on its content, so with that I assume it's impossible. For the kicks and giggles, though, I checked the storyboard and there is no property that allows your NSTextField to be resized besides the auto resizing mask, which have nothing to do with the NSTextField text attribute.

其他提示

Try like this:- connect delegate to your textfield

-(void)controlTextDidChange:(NSNotification *)obj
{
    NSString *str=[yourTextField stringValue];
    NSCell *cell=[[NSCell alloc]initTextCell:str];
    CGFloat sz=[cell cellSize].width;
    [yourTextField setFrameSize:NSMakeSize(sz+10.0, 22.0)];
}

Try to set the preferredMaxLayoutWidth for NSTextfield, this value will be used to update your height, I hope it is helpful. And Reference resources:UILabel sizeToFit doesn't work with autolayout ios6

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top