Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
JH95 Avatar asked Dec 20 '12 13:12

JH95


2 Answers

If your tfScroll is NSTextField:

CGRect frame = tfScroll.frame;
frame.size.width = tfScroll.attributedStringValue.size.width+somepoints;//(somepoints=8)
tfScroll.frame = frame;

I did not find NSTextfield.contentSize.

like image 83
DaoYan.Tang Avatar answered Nov 16 '22 17:11

DaoYan.Tang


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;
like image 40
CainaSouza Avatar answered Nov 16 '22 17:11

CainaSouza