<?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 Rob Huebner on Medium]]></title>
        <description><![CDATA[Stories by Rob Huebner on Medium]]></description>
        <link>https://medium.com/@robert.huebner?source=rss-a5c7fcdcab2e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AfC0sDTT9S3nh9G8aqKGfA.jpeg</url>
            <title>Stories by Rob Huebner on Medium</title>
            <link>https://medium.com/@robert.huebner?source=rss-a5c7fcdcab2e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:17:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@robert.huebner/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[Styling Text on iOS: The Kerning Point]]></title>
            <link>https://medium.com/@robert.huebner/styling-text-on-ios-the-kerning-point-b373cabfaca5?source=rss-a5c7fcdcab2e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b373cabfaca5</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[polish]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Rob Huebner]]></dc:creator>
            <pubDate>Wed, 30 Aug 2017 14:01:01 GMT</pubDate>
            <atom:updated>2017-08-30T14:01:01.169Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SlXUBtDz7qV3Y5qHyauMcg.jpeg" /></figure><p>On an otherwise unremarkable Wednesday morning, a ping from Slack interrupted the noise-cancelled silence. It’s design. They’re asking for a small tweak to our fonts. “Oh yeah, and we just need to adjust all the tracking a bit too.”</p><p>Uh oh. 😳</p><h3>Fundamental Attribution</h3><p>To understand why this was a big ask, consider the most basic way we display text on iOS: using a UILabel.</p><p>If you’ve worked with UIKit, you know the ins and outs of UILabel. It&#39;s got a few properties to customize the appearance of text, like font, textColor, and textAlignment. But, after that, you start hitting a wall. Doing anything fancier usually requires creating an NSAttributedString with both your text and appropriate attributes, then using the label&#39;s attributedText property.</p><p>That seems okay enough, but then you try to scale this approach and discover some rough edges:</p><ol><li>Making attributed strings everywhere feels pretty clunky and unnecessary.</li><li>If your label’s text is dynamic, you need to remake the whole attributed string every time you want to change the text, even though the style didn’t change.</li><li>There’s no great way to standardize the styles of your strings, or to build on top of default values.</li></ol><p>Well, tracking, or <em>kerning</em> as Apple calls it*, is one of those parameters that can only be specified using attributed strings. So, if we want to adjust our font’s kerning globally, we need to use attributedText globally. Unfortunately, this is how an innocent request to tweak font spacing app-wide can lead to a dizzying leap in code complexity wherever we display text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XpyCzxnwA1SuV0Q_vFvsdA.png" /></figure><p>Let’s build a better font system.</p><h3>Text Styles</h3><p>We don’t really want to manually set up attributed strings everywhere. Ideally, our static text style would be declared independently from the text itself. Then, we can set the style early in the lifecycle of our label, and we don’t have to worry about it again.</p><p>To that end, we wrote StyledText, a declarative text styling library for iOS. StyledText introduces two new concepts: TextStyle, a type that represents a specific set of text styling parameters; and Styled UIView subclasses, which accept styles and use them to draw text content. These improvements allow us to follow the pattern we want, just create a TextStyle up front and pass it to a StyledLabel:</p><pre>class ViewController: UIViewController {<br>    private let styledLabel: StyledLabel = {<br>        let label = StyledLabel(frame: .zero)<br>        label.textStyle = TextStyle(font: .systemFont(ofSize: 14), <br>                                   color: .blue, <br>                             lineSpacing: 4.0, <br>                                    kern: 1.5)<br>        return label<br>    }()<br>    <br>    func updateLabel() {<br>        styledLabel.text = &quot;This is a simple string&quot;<br>    }<br>}</pre><h3>Sensible Defaults</h3><p>Well, that’s great and all, but it still doesn’t solve our ultimate dilemma. How do we tweak kerning <em>app wide</em>? The answer again lies in TextStyle, this time in something we call a TextStyleDefaultsGenerator. When we set up one of these, it gets asked for a default value for kern whenever a style is initialized without one specified.</p><pre>extension TextStyle: TextStyleDefaultsGenerator {<br>    private static let defaultCeraKern: CGFloat = -0.2<br>    private static let defaultChronicleKern: CGFloat = -0.2</pre><pre>    static public func defaultKern(for font: UIFont) -&gt; CGFloat? {<br>        if font.fontName.contains(&quot;Cera&quot;) {<br>            return defaultCeraKern<br>        } else if font.fontName.contains(&quot;Chronicle&quot;) {<br>            return defaultChronicleKern<br>        }<br>        return nil<br>    }<br>}</pre><p>Problem solved. 😎</p><p>By fully adopting text styles in the <a href="https://itunes.apple.com/us/app/blue-apron/id976642810?mt=8">Blue Apron iOS App</a>, we were able to tweak the finest details of our fonts globally with minimal increase in overhead. We’re excited to release <a href="https://github.com/blueapron/styled-text"><strong>StyledText</strong></a> as an open source library to the iOS community. We hope to continue building it into a powerful typographical tool for the mobile platform.</p><h3>Bonus: Free Accessibility Win</h3><p>Now that we have all our text styling centralized, there’s a lot of cool things we can do. For example, we can build support for Dynamic Type, which is Apple’s framework for letting the user change their system font size. So we did just that! Included for free in every box of StyledText is the ability to specify a dynamic type behavior on every TextStyle. Pick a size, any size:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*b3ytduAfQ3Rn2k_o4VdyLQ.gif" /></figure><h3>Style your Text</h3><p><a href="https://github.com/blueapron/styled-text">Start with our README on GitHub!</a> Check us out today and let us know what you think, contributions welcomed and encouraged!</p><p>[*] Typography actually defines “kerning” as the spacing between a set of two specific letters. For example, ‘A’ and ‘V’ are specifically pushed closer together in most fonts because their shapes can overlap. “Tracking” is how the character spacing is offset over a larger set of arbitrary characters. But Apple provides only the string attribute NSKernAttributeName to refer to both concepts, so that’s what StyledText uses as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b373cabfaca5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>