Search code examples
objective-cxcodemacoscocoanstextfield

Updating the width of NSTextField based on length of string


I want to make a label that change its size depending on the size of the string value that it will show. Currently I am doing this:

[tfScroll setStringValue:strScoller];
[tfScroll sizeToFit];

However this is not working. What am I missing?


Solution

  • If you trying to resize the fields's height, this may works:

    CGRect frame = tfScroll.frame;
    frame.size.height = tfScroll.contentSize.height;
    tfScroll.frame = frame;