{"id":17238,"date":"2015-03-30T12:00:36","date_gmt":"2015-03-30T16:00:36","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=17238"},"modified":"2015-03-30T12:00:36","modified_gmt":"2015-03-30T16:00:36","slug":"creating-beautiful-ios-controls-with-paintcode","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/creating-beautiful-ios-controls-with-paintcode\/","title":{"rendered":"Creating Beautiful iOS Controls with PaintCode"},"content":{"rendered":"<p>\t\t\t\tDelivering a beautifully designed app can be the difference between having an app that gets used, or having an app that no one notices. Of course, having a beautiful app doesn&#8217;t just happen &#8211; creating something different often means creating your own custom controls, and creating beautiful custom controls on iOS can be a difficult task without intimate knowledge of CoreGraphics. This is where PaintCode steps in. PaintCode is a vector drawing app that generates C# code in real-time, allowing you to design your iOS controls and export the resulting &#8220;drawing code&#8221;. This is great because it allows you to create resolution-independent controls that will look great on any size of device.<\/p>\n<p>From the beginning, the Xamarin Designer for iOS has supported real-time\u00a0rendering of custom controls, which allows you to see instant feedback on your code changes. A number of developers have been opting to use images for control customization, which leads to a series of issues including bloating your app store binary size and\u00a0that these images simply won&#8217;t scale properly on new screen sizes that Apple may release in the future.<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2015-03-06-at-14.55.17.png\"><img decoding=\"async\" class=\"aligncenter wp-image-17240 \" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2015-03-06-at-14.55.17-1024x652.png\" alt=\"PaintCode on Mac\" width=\"858\" height=\"546\" \/><\/a><\/p>\n<h4>StyleKit<\/h4>\n<p>PaintCode can generate a special class that<img decoding=\"async\" src=\"http:\/\/pixel-geo.prfct.co\/cs\/?partnerId=twtr\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><img decoding=\"async\" src=\"http:\/\/pixel-geo.prfct.co\/cs\/?partnerId=yah\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><img decoding=\"async\" src=\"http:\/\/pixel-geo.prfct.co\/cs\/?partnerId=goo\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><img decoding=\"async\" src=\"http:\/\/pixel-geo.prfct.co\/cs\/?partnerId=fbx\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> contains all of the\u00a0drawings, colors, gradients, shadows, and other design assets used in your app.\u00a0In the following example, I&#8217;ve named the StyleKit <code>DemoStyleKit<\/code>. You&#8217;ll want to make sure you&#8217;re regularly exporting the StyleKit from PaintCode into your Xamarin project in order for changes to be made available to you in your project.\u00a0Exporting your style sheet will create a <b>.cs<\/b> style and copy any images your styles use into the directory of your choice. Once you&#8217;ve done this, you&#8217;ll need to add these to your existing Xamarin.iOS project.<\/p>\n<h4>Creating a custom control<\/h4>\n<p>Once you&#8217;ve added the exported files, you&#8217;re ready to consume the drawing code in a custom control. You will need to create a new class which will be your custom control; in this sample I&#8217;ve called it &#8216;MyTimerView&#8217; and it inherits from UIView.<\/p>\n<p>It&#8217;s here where we&#8217;ll consume the auto-generated C# from PaintCode and set it up to support the Storyboard designer. In this example, I annotate the class with the\u00a0DesignTimeVisible attribute. This tells our iOS Storyboard designer that I\u00a0wish to use this class within the Storyboard designer. A\u00a0basic class\u00a0with Storyboard support\u00a0should look something like this:<\/p>\n<pre class=\"lang:csharp decode:true\">\n[Register(&quot;MyTimerView&quot;), DesignTimeVisible(true)]\npublic class MyTimerView : UIView\n{\n  public MyTimerView(IntPtr p) : base(p)\n  {\n    Initialize ();\n  }\n\n  public MyTimerView ()\n  {\n   Initialize ();\n  }\n\n  void Initialize ()\n  {\n  }\n\n  public override void Draw(CoreGraphics.CGRect rect)\n  {\n   base.Draw(rect);\n  }\n}\n<\/pre>\n<p>Because PaintCode\u00a0exports the drawing code into the StyleKit, we can implement the drawing with just one line of code!<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override void Draw(CoreGraphics.CGRect rect)\n{\n  DemoStyleKit.DrawTimer(rect, Angle);\n}\n<\/pre>\n<p>If you save and build the project now and open your Storyboard, you should find MyTimerView within the control toolbox. You can drag and drop this into your view and instantly see how it looks.<\/p>\n<h4><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2015-03-06-at-16.00.57.png\"><img decoding=\"async\" class=\"aligncenter wp-image-17242\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2015-03-06-at-16.00.57-1024x845.png\" alt=\"A custom control in the iOS storyboard designer\" width=\"858\" height=\"708\" \/><\/a>Advanced properties<\/h4>\n<p>It would be great to be able to control the angle of the green progress line from the Properties panel within our IDE. Fortunately,\u00a0custom controls allow for design time properties for a number of types, including string, bool, SizeF, UIColor, and UIImage. To add an angle property to our properties pad, let&#8217;s add the following to our class:<\/p>\n<pre class=\"lang:csharp decode:true\">\nfloat angle;\n[Export(&quot;Angle&quot;), Browsable(true)]\npublic float Angle\n{\n  get { return angle; }\n  set\n  {\n    angle = value;\n    SetNeedsDisplay ();\n  }\n}\n<\/pre>\n<p>It&#8217;s also worth ensuring that the _angle property has an initial value by setting it in the\u00a0<span style=\"font-family: Menlo\"><span style=\"color: #333333\">Initialize<\/span><\/span> method. Once you&#8217;ve done this, you&#8217;ll see a new property available to you and the changes to the value will automatically be updated in the custom control. This allows you to create very customizable controls without needing to drop into code to make tweaks.<\/p>\n<h4>Learn more<\/h4>\n<p>You can learn more about PaintCode on their <a href=\"http:\/\/www.paintcodeapp.com\/\">website<\/a>\u00a0and purchase the\u00a0app from the\u00a0<a href=\"https:\/\/itunes.apple.com\/gb\/app\/paintcode-2\/id808809998?mt=12&amp;uo=4&amp;at=10l4KJ&amp;ct=paintcodesite\">Mac App Store.<\/a><\/p>\n<p>To get started, download the sample project from this blog post from <a href=\"https:\/\/github.com\/MichaelJames6\/PaintCodeDemo\/tree\/master\">GitHub<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delivering a beautifully designed app can be the difference between having an app that gets used, or having an app that no one notices. Of course, having a beautiful app doesn&#8217;t just happen &#8211; creating something different often means creating your own custom controls, and creating beautiful custom controls on iOS can be a difficult [&hellip;]<\/p>\n","protected":false},"author":1929,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-17238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Delivering a beautifully designed app can be the difference between having an app that gets used, or having an app that no one notices. Of course, having a beautiful app doesn&#8217;t just happen &#8211; creating something different often means creating your own custom controls, and creating beautiful custom controls on iOS can be a difficult [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/17238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1929"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=17238"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/17238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=17238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=17238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=17238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}