<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Srđan Rašić on Medium]]></title>
        <description><![CDATA[Stories by Srđan Rašić on Medium]]></description>
        <link>https://medium.com/@srdanrasic?source=rss-f4127caa4321------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LoF0vz5eZ4dzpI11gRK2cQ.jpeg</url>
            <title>Stories by Srđan Rašić on Medium</title>
            <link>https://medium.com/@srdanrasic?source=rss-f4127caa4321------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 07:58:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srdanrasic/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simple Stylesheets in Swift]]></title>
            <link>https://medium.com/hackernoon/simple-stylesheets-in-swift-6dda57b5b00d?source=rss-f4127caa4321------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dda57b5b00d</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[simple-stylesheets]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[stylesheets-in-swift]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Srđan Rašić]]></dc:creator>
            <pubDate>Sat, 09 Dec 2017 22:22:31 GMT</pubDate>
            <atom:updated>2019-05-20T04:27:38.505Z</atom:updated>
            <content:encoded><![CDATA[<p>Does your view code look like this?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/126e3783385a7dafea3f36352ce1e01b/href">https://medium.com/media/126e3783385a7dafea3f36352ce1e01b/href</a></iframe><p>If the answer is yes then you are like me until recently when an idea crossed my mind. The idea which I find so beautiful that I need to share it with all of you.</p><p>The code in the closures represents the respective subviews’ styles. It is nothing more than that, yet it clutters the view class interface and implementation. Moreover, the same or similar code is often duplicated throughout the app <a href="https://hackernoon.com/tagged/codebase">codebase</a>. How do we make it better? By doing the only logical thing — moving such code out of the view classes.</p><p>A way to do that is very simple. We will stick with the closures as in the snippet above, but with a minor change — we will not instantiate view in the closure, rather we will give the closure an instance of the view to style. We will also wrap that closure in a struct so that we can easier work with it. The struct shall be known as Style&lt;View&gt;.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14201f5ec3f57f386e1fced7756226ac/href">https://medium.com/media/14201f5ec3f57f386e1fced7756226ac/href</a></iframe><p>It is just a struct that wraps single-argument closure. There is a restriction that the argument type has to be UIView or its subclass.</p><p>We can now style a view by passing it to the apply method. However, let us make it even simpler by defining few extensions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f6788270edbe53d493cd2a4db8ef2d3/href">https://medium.com/media/0f6788270edbe53d493cd2a4db8ef2d3/href</a></iframe><p>Now we can do it the other way around, but also using the convenience initializer. Unfortunately, there are limitations to <a href="https://hackernoon.com/tagged/swift">Swift</a> generics because covariance is not generally supported and we want to be able to style, for example, an instance of UILabel with an instance of Style&lt;UIView&gt;. For that reason we have to cast. A small price to pay.</p><p>We now have everything needed to improve our view classes — move the code that represents style into a stylesheet type. It will just be an enum that provides us with a namespace. We can group related styles into another nested enum. This is how it can look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab32e1602a48917693246e9929bd50dd/href">https://medium.com/media/ab32e1602a48917693246e9929bd50dd/href</a></iframe><p>How you define stylesheet is completely up to you. It could be one stylesheet for the whole app or it could be separate stylesheets per modules. You can add styles or groups of styles using extensions and you can define reusable or private styles. It is very flexible approach, simple and, best of all, makes view classes extremely clean:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/104b0315758fa762ec8bb4c8c23bc7a7/href">https://medium.com/media/104b0315758fa762ec8bb4c8c23bc7a7/href</a></iframe><p>The presented solution greatly resembles how styling is done on the web — an aproach that has stood the test of time. You can easily take all this to the next level by defining few extensions on Style type that provide inheritance and other ways of composition that will enable to you easily define global or shared styles.</p><p>I would love to hear you feedback so feel free to leave a comment if you like it or have any conerns.</p><p>The presented solution is part of the <a href="https://github.com/AbsurdAbstractions/Layoutless">Layoutless</a> library. Apart from declarative styling, it provides a way to make simple declarative layout.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dda57b5b00d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/simple-stylesheets-in-swift-6dda57b5b00d">Simple Stylesheets in Swift</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>