{"id":1690,"date":"2006-10-02T17:19:53","date_gmt":"2006-10-02T20:19:53","guid":{"rendered":"http:\/\/brockerhoff.net\/bb\/viewtopic.php?p=1982"},"modified":"2010-05-08T19:30:44","modified_gmt":"2010-05-08T22:30:44","slug":"autoresizing-nstextfields","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2006\/10\/02\/autoresizing-nstextfields\/","title":{"rendered":"Autoresizing NSTextFields"},"content":{"rendered":"<p>So, I needed to autoresize NSTextFields in <a href=\"\/\">XRay<\/a> II (vertically only). Sort of a poor man&#8217;s WebView. This had to &#8220;just work&#8221; on certain NSTextFields used by third-party plugins, though, without any extra code or subclassing by the plugin writer.<\/p>\n<p>There are two problems there. One is finding out the actual optimum vertical size, while editing and while not, for any type of border or bezel. Here&#8217;s the code I finally worked out, with the kind help of <a href=\"http:\/\/www.red-sweater.com\/\">Daniel Jalkut<\/a>:<\/p>\n<pre><code>- (NSSize)minSizeForContent {\r\n\u00a0 \u00a0NSRect frame = [self frame];\r\n\u00a0 \u00a0NSRect newf = frame;\r\n\u00a0 \u00a0NSTextView* editor = nil;\r\n\u00a0 \u00a0if ((editor = (NSTextView*)[self currentEditor])) {\r\n\u00a0 \u00a0\u00a0 \u00a0newf = [[editor layoutManager] usedRectForTextContainer:[editor textContainer]];\r\n\u00a0 \u00a0\u00a0 \u00a0newf.size.height += frame.size.height-[[self cell] drawingRectForBounds:frame].size.height;\r\n\u00a0 \u00a0} else {\r\n\u00a0 \u00a0\u00a0 \u00a0newf.size.height = HUGE_VALF;\r\n\u00a0 \u00a0\u00a0 \u00a0newf.size = [[self cell] cellSizeForBounds:newf];\r\n\u00a0 \u00a0}\r\n  \u00a0frame.size.height = newf.size.height;\r\n\u00a0 \u00a0return frame.size;\r\n}<\/code><\/pre>\n<p>So I put this into a category of NSTextField and did some runtime diddling with implementation pointers, but for other uses it could well be in a subclass.<\/p>\n<p>The second problem is properly pushing down the views below the field when it is resized. The solution I ended up coding is a little too gnarly to post here, and it depends on the field and its sibling views being inside a custom NSView subclass with flipped coordinates&#8230; still, it seems to work well enough now, so I&#8217;ll leave it there and work on other stuff.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I needed to autoresize NSTextFields in XRay II (vertically only). Sort of a poor man&#8217;s WebView. This had to &#8220;just work&#8221; on certain NSTextFields used by third-party plugins, though, without any extra code or subclassing by the plugin writer. There are two problems there. One is finding out the actual optimum vertical size, while [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19],"tags":[26,29],"class_list":["post-1690","post","type-post","status-publish","format-standard","hentry","category-software","tag-cocoa","tag-xray"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-rg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=1690"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1690\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=1690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=1690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}