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.
How to let the width of an NSTextField be adjusted to its content?
-
29-06-2022 - |
题
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
其他提示
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
不隶属于 StackOverflow