How to resize NSTextField dynamically?

落爺英雄遲暮 提交于 2019-12-11 17:34:07

问题


I want an NSTextField to get bigger as the user types, and smaller as backspace is pressed etc.

The only thing I've found is [textField sizeToFit] but that doesn't run dynamically, it's just a one off resizing. Is there a property or something I need to set?

p.s. I only need it to resize horizontally (single line text).


回答1:


Set a delegate on the NSTextField which implements

- (void)controlTextDidChange:(NSNotification *)aNotification

Every time the delegate gets called resize the control per your idea above.




回答2:


What Richard said, except you need to know by how much to resize your textfield. To get this value, use the the layout manager of the NSTextField and call something like boundingRectForGlyphRange:inTextContainer: on it with a range of all glyphs. This should tell you the dimensions of the rectangle bounding all of your text. Take these dimensions and set them to the width and height of your NSTextField content.



来源:https://stackoverflow.com/questions/12705316/how-to-resize-nstextfield-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!