{"id":381,"date":"2016-01-03T15:36:36","date_gmt":"2016-01-03T15:36:36","guid":{"rendered":"http:\/\/strawberrycode.com\/blog\/?p=381"},"modified":"2016-01-03T15:36:36","modified_gmt":"2016-01-03T15:36:36","slug":"swift-tips-2-setborder-extension","status":"publish","type":"post","link":"https:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/","title":{"rendered":"Swift Tips #2: setBorder Extension"},"content":{"rendered":"<p>Happy New Year! This is my first post of 2016, so I would like to thank you all for reading my blog and to wish you a very Happy New Year. May this year be as bug free as possible in your apps as well as in your life!<\/p>\n<p>As a New Year gift, I&#8217;d like to share with you a little something that I&#8217;m using all the time: my <code>setBorder<\/code> Extension. It is the first member of my &#8220;Extensions essentials&#8221;.<\/p>\n<p>When I integrate a design or debug something, I want to see quickly where my elements are, so I set a border to the ones I&#8217;d like to see. An old web habit perhaps, but I find it very useful. <\/p>\n<p>Without further ado, here comes the code: <\/p>\n<pre class=\"lang:swift decode:true \" title=\"setBorder Extension\" >extension UIView {\r\n    \r\n    func setBorder(color: UIColor, width: CGFloat = 1.0) {\r\n        self.layer.borderColor = color.CGColor\r\n        self.layer.borderWidth = width\r\n    }\r\n}<\/pre>\n<p>You can place it wherever you want in the code. Personally, I like to create an <code>Extension.swift<\/code> file at the root of my project and put all my extensions there. As it is an extension of <code>UIView<\/code>, it works on all kinds of views: <code>UIView<\/code>, <code>UIImage<\/code>, <code>UILabel<\/code>, <code>UITableView<\/code>, <code>UICollectionViewCell<\/code>, &#8230; <\/p>\n<p>To use the <code>setBorder<\/code> extension, it&#8217;s pretty easy: <\/p>\n<pre class=\"lang:swift decode:true \" title=\"How to use setBorder\" >myImage.setBorder(UIColor.redColor()) \/\/ red border with default width\r\nmyTable.setBorder(UIColor.blueColor(), width: 5) \/\/ large blue border<\/pre>\n<p>Obviously, you can customize it and unleash the power of the border by adding a default color value, or passing hexadecimal colors (I combined it with <a href=\"https:\/\/github.com\/thii\/SwiftHEXColors\" target=\"_blank\">SwiftHEXColors<\/a> on a project, it works beautifully!).<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Happy New Year! This is my first post of 2016, so I would like to thank you all for reading my blog and to wish you a very Happy New Year. May this year be as bug free as possible in your apps as well as in your life! As a New Year gift, I&#8217;d like to share with you a little something that I&#8217;m using all the time: my setBorder Extension. It is the first member of my &#8220;Extensions essentials&#8221;. When I integrate a design or debug something, I want to see quickly where my elements are, so I &hellip; <a href=\"https:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Swift Tips #2: setBorder Extension<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[63],"tags":[76,17,75,41,74],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift Tips #2: setBorder Extension - StrawberryCode<\/title>\n<meta name=\"description\" content=\"When I integrate a design or debug something, I want to see quickly where my elements are, an old web habit perhaps... so I created a setBorder extension.\" \/>\n<link rel=\"canonical\" href=\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Tips #2: setBorder Extension - StrawberryCode\" \/>\n<meta property=\"og:description\" content=\"When I integrate a design or debug something, I want to see quickly where my elements are, an old web habit perhaps... so I created a setBorder extension.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\" \/>\n<meta property=\"og:site_name\" content=\"StrawberryCode\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-03T15:36:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"StrawberryCode\",\"description\":\"Fruit for Thought\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/strawberrycode.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/#primaryimage\",\"inLanguage\":\"en-GB\",\"url\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\",\"contentUrl\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2015\/04\/Apple_Swift_Logo-e1430073805246.png\",\"width\":200,\"height\":200,\"caption\":\"Apple Swift Logo\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/#webpage\",\"url\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\",\"name\":\"Swift Tips #2: setBorder Extension - StrawberryCode\",\"isPartOf\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/#primaryimage\"},\"datePublished\":\"2016-01-03T15:36:36+00:00\",\"dateModified\":\"2016-01-03T15:36:36+00:00\",\"author\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\"},\"description\":\"When I integrate a design or debug something, I want to see quickly where my elements are, an old web habit perhaps... so I created a setBorder extension.\",\"breadcrumb\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\",\"url\":\"http:\/\/strawberrycode.com\/blog\/swift-tips-2-setborder-extension\/\",\"name\":\"Swift Tips #2: setBorder Extension\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\",\"name\":\"StrawberryCode\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/381"}],"collection":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":5,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":386,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions\/386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}