{"id":148,"date":"2006-06-17T20:43:42","date_gmt":"2006-06-18T00:43:42","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/148\/what-a-difference-a-cell-makes"},"modified":"2018-09-17T19:25:35","modified_gmt":"2018-09-17T23:25:35","slug":"what-a-difference-a-cell-makes","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/148\/what-a-difference-a-cell-makes","title":{"rendered":"What a Difference a Cell Makes"},"content":{"rendered":"<p>\nI&#8217;ve been bothered for a long time by a limitation of NSTextFieldCell that prevents developers from specifying a vertically centered alignment. This is particular annoying in contexts where Apple practically rolls out the red carpet for a particular UI feature, but where taking advantage of that feature is almost guaranteed to produce an ugly result. Take NSTableView for example. What is the most common cell type in a table? NSTextFieldCell of course. But what happens when you add the slightest bit of creativity to a table, say by changing the row height to a value not suited to the font size?\n<\/p>\n<p>\n<img decoding=\"async\" width=\"500\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/VertTextBadAlign.jpg\"\/>\n<\/p>\n<p>\nThat&#8217;s bad enough,  but let&#8217;s say you find good reason to reach for even taller table columns? It can only get worse from here:\n<\/p>\n<p>\n<img decoding=\"async\" width=\"500\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/VertTextWorstAlign.jpg\"\/>\n<\/p>\n<p>\nIn the past I&#8217;ve just worked around this problem by finding another way to present my UI. In other words: just keep the freakin&#8217; table view rows shallow, or else live with the funny looking top-alignment of the text. Today I got fed up with that and decided to shoot for a general-purpose solution.\n<\/p>\n<p>\nI didn&#8217;t want to implement my own completely custom text cell. NSTextFieldCell is pretty good, you know! It does a lot for me, and I&#8217;d like to continue taking advantage of that. Through a bit of experimentation, I discovered that I could make a pretty minor tweak to NSTextFieldCell and get the behavior I wanted. By overriding &#8220;drawingRectForBounds:&#8221; and returning, instead of the whole lot of real estate available to me, a centered subset of that area, I convinced AppKit to render the text in the middle like I wanted it:\n<\/p>\n<p>\n<img decoding=\"async\" width=\"500\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/VertTextGoodAlign.jpg\"\/>\n<\/p>\n<p>\nHey! That&#8217;s looking a lot better. I like it. It looks pretty. You know, like a Mac. But you know what doesn&#8217;t look pretty? When I double-click this bad boy and try to edit the text:\n<\/p>\n<p>\n<img decoding=\"async\" width=\"500\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/VertTextBadEdit.jpg\"\/>\n<\/p>\n<p>\nOuch! That&#8217;s a pretty freaky looking user experience. Apparently NSTextFieldCell or some other component of AppKit is not used to having the drawing rect drastically adjusted without its knowledge. I might have to call this whole deal off. However bad top-aligned text in my table cell is, it&#8217;s a bajillion times less offensive than that. But then I realized that as a cell, I am given a lot of control over the edit process. What if I can limit my rect adjustment so that it&#8217;s normal while we&#8217;re editing, but weird (i.e. good) all the rest of the time?\n<\/p>\n<p>\nI added two methods to my custom subclass, overriding &#8220;selectWithFrame:&#8230;&#8221; and &#8220;editWithFrame:&#8230;&#8221;.  In these methods, I use my rect calculation method to transform the proposed edit or selection rect into the more desirable &#8220;centered rect,&#8221; then I set a flag in the class &#8220;mIsEditingOrSelecting&#8221; which tells my rect calculator to stop the funny business. The result is an NSTextFieldCell with very little code that, as far as I can tell, behaves appropriately in all circumstances:\n<\/p>\n<p>\n<img decoding=\"async\" width=\"500\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/VertTextGoodEdit.jpg\"\/>\n<\/p>\n<p>\nGood enough for beta software! But here&#8217;s where you come in. You want this sexy vertically aligned cell for your app, right? So let&#8217;s share and figure out whether it&#8217;s monumentally busted or not.\n<\/p>\n<p>\nDownload <a href=\"http:\/\/www.red-sweater.com\/blog\/downloads\/RSVerticallyCenteredTextFieldCell.zip\">RSVerticallyCenteredTextFieldCell<\/a>. MIT License. Pop it into your project and, upon waking from nib, set your text column data cells (or other text field cells, if you have good reason) to instances of it. Let me know if you spot any bugs!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been bothered for a long time by a limitation of NSTextFieldCell that prevents developers from specifying a vertically centered alignment. This is particular annoying in contexts where Apple practically rolls out the red carpet for a particular UI feature, but where taking advantage of that feature is almost guaranteed to produce an ugly result. [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,29,15],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-free-code","category-programming"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":1,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":3374,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions\/3374"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}