NSTextFieldCell's cellSizeForBounds: doesn't match wrapping behavior?

后端 未结 1 1914
小鲜肉
小鲜肉 2021-01-16 23:34

It seems to be commonly accepted that cellSizeForBounds: allows one to calculate a text field\'s \"natural\" size. However, for NSTextField, I\'ve found that it

相关标签:
1条回答
  • 2021-01-17 00:08

    There is a difference between the cell size and the frame size. The best way to determine it is to ask a text field to size itself to its content using -sizeToFit and then compare its cell size to its frame size. You may want to do this with a secondary, off-screen text field. Be sure to configure all of its parameters identically to the text field you're intending to resize. Also, the cell size will be an "exact" fit, meaning it will potentially have fractional width or height. The frame size resulting from -sizeToFit will be integral. So, you should apply ceil() to the cell size components before comparing to the frame size to compute the border size.

    In theory, you only have to do this once for a given text field configuration / style.

    0 讨论(0)
提交回复
热议问题