{"id":44,"date":"2010-05-05T22:32:43","date_gmt":"2010-05-06T06:32:43","guid":{"rendered":"http:\/\/bynomial.com\/blog\/?p=44"},"modified":"2010-05-05T22:32:43","modified_gmt":"2010-05-06T06:32:43","slug":"drop-shadows-on-text","status":"publish","type":"post","link":"http:\/\/bynomial.com\/blog\/?p=44","title":{"rendered":"Drop shadows on text"},"content":{"rendered":"<p>UILabel is built with a handy shadow feature, but it doesn&#8217;t always look the way you might want &#8211; in this post I&#8217;ll describe the<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">ShadowedLabel<\/div><\/div>\n<p>subclass which draws nicer shadows. \u00c2\u00a0<em>(Code links below.)<\/em><\/p>\n<h2>Default UILabel shadows<\/h2>\n<p>This code:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-45\" title=\"UILabel_code1\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/UILabel_code1.png\" alt=\"\" width=\"460\" height=\"95\" srcset=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/UILabel_code1.png 460w, http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/UILabel_code1-300x61.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n<p>generates a shadow that looks like this:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-46\" title=\"UILabel_shadow\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/UILabel_shadow.png\" alt=\"\" width=\"125\" height=\"41\" \/><\/p>\n<p>It&#8217;s not the end of the world, but it&#8217;s still kind of a butter shadow. \u00c2\u00a0I use these more for relatively subtle accents on labels in buttons. \u00c2\u00a0For example, you can improve the appearance of a label on a button by making the text look more embossed:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-47\" title=\"UILabel_embossed_look\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/UILabel_embossed_look.png\" alt=\"\" width=\"259\" height=\"47\" \/><\/p>\n<p>I think the text on the left, with the shadow slightly above the text, makes it appear depressed into the surface, which is more visually appealing than the plain text on the right. \u00c2\u00a0Apple consistently works with their highlights and shadows in iPhone OS as if there is a global light source directly above the device, so users are likely to interpret shadows according to a straight-above light.<\/p>\n<h2>ShadowedLabel<\/h2>\n<p>The<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">ShadowedLabel<\/div><\/div>\n<p>is a drop-in replacement of<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">UILabel<\/div><\/div>\n<p>(a subclass of it) which automatically draws dark, slightly-translucent, shadows beneath all text. \u00c2\u00a0This code:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-48\" title=\"ShadowedLabel_code1\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/ShadowedLabel_code1.png\" alt=\"\" width=\"653\" height=\"69\" srcset=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/ShadowedLabel_code1.png 653w, http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/ShadowedLabel_code1-300x31.png 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/p>\n<p>generates this:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-49\" title=\"ShadowedLabel_regular\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/ShadowedLabel_regular.png\" alt=\"\" width=\"239\" height=\"39\" \/><\/p>\n<p>Much nicer!<\/p>\n<p>The color, position, and blur can all be adjusted. \u00c2\u00a0For example, you could use the same class, with slightly changed parameters, to get this result:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-50\" title=\"ShadowedLabel_weird\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/05\/ShadowedLabel_weird.png\" alt=\"\" width=\"241\" height=\"37\" \/><\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">ShadowedLabel<\/div><\/div>\n<p>works by overriding the<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">drawTextInRect:<\/div><\/div>\n<p>method of<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">UILabel<\/div><\/div>\n<p>, along with the<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">CGContextSetShadowWithColor<\/div><\/div>\n<p>function. \u00c2\u00a0Say goodbye to butter shadows!<\/p>\n<h2>Code and References<\/h2>\n<p><a href=\"http:\/\/bynomial.com\/blogfiles\/ShadowedLabel.h\">ShadowedLabel.h<\/a><\/p>\n<p><a href=\"http:\/\/bynomial.com\/blogfiles\/ShadowedLabel.m\">ShadowedLabel.m<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1274168\/drop-shadow-on-uitextfield-text\/1537079#1537079\">Related stackoverflow question<\/a><\/p>\n<p><a href=\"http:\/\/developer.apple.com\/iPhone\/library\/documentation\/GraphicsImaging\/Conceptual\/drawingwithquartz2d\/dq_shadows\/dq_shadows.html#\/\/apple%5Fref\/doc\/uid\/TP30001066-CH208-TPXREF101\">Quartz 2D shadow reference<\/a><\/p>\n<p><a href=\"http:\/\/developer.apple.com\/iphone\/library\/documentation\/UIKit\/Reference\/UILabel_Class\/Reference\/UILabel.html#\/\/apple_ref\/occ\/instm\/UILabel\/drawTextInRect:\">UILabel drawTextInRect: reference<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UILabel is built with a handy shadow feature, but it doesn&#8217;t always look the way you might want &#8211; in this post I&#8217;ll describe the ShadowedLabel subclass which draws nicer shadows. \u00c2\u00a0(Code links below.) Default UILabel shadows This code: generates a shadow that looks like this: It&#8217;s not the end of the world, but it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[14,9],"tags":[50,51],"_links":{"self":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44"}],"collection":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=44"}],"version-history":[{"count":0,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}