SKCore – A Simplification Toolset (Core Graphics, Autolayout and more)

What is SKCore?

SKCore is a set of classes and categories created to simplify layout and drawing in code. The objective of SKCore is to take underlying technologies and make simple wrappers around them to reduce the amount of code needed.

SKCore GitHub Repo

Drawing

One of the most powerful things that SKCore simplifies is drawing with Core Graphics. Some of the messiest code I have come across is drawing code. Part of the reason for this, is that it is written in C instead of Objective-C and C tends to get verbose. SKCore offers some simple Objective-C wrappers that can make drawing code a lot easier to read, maintain and update. A good use case for this is to create a generic button skin that can be used across your app without the need to include an image asset. This is useful when the design for something is simple to recreate in code. Small design tweaks and color changes can then be as simple as changing a hex value or measurement variable. Below is a link to a gist that demonstrates how to make a stretchable button skin with SKCore. The resulting output can be seen in the image below.

View Drawing Code

SKCore_Button

Layout

SKCore has a set of categories to simplify the management and creation of Auto Layout constraints. The main problem with Auto Layout, in my opinion, is the verbose nature of the methods used to create constraints. Below is an example which shows the code needed for the layout of a screen using default methods. It also shows the SKCore alternative. Both ways used the exact same VFL strings to create the layout. The SCKore version cuts the code down from 2,058 characters to just 582. The result can be seen in the image below.

Auto Layout Comparison Code

SKCore_Layout

UITextField attributedPlaceholder Doesn’t Work

The Problem

In my efforts to build an easier way to create forms I stumbled across and interesting bug in UIKit. Apparently the attributedPlaceholder property doesn’t work. Not only does it not display based on the attributes you set, but it also strips them out completely. This means to retain access to the attributes you set, you have to override the setter and store it before it is tainted.

The Solution

Note: Keep in mind that this fix has only been tested on iOS 6.0 and up.

To work around this bug, you have to create a subclass of UITextField and override the -setAttributedPlaceHolder to store the result in an ivar. Then you have to override the -drawPlaceholderInRect method and enumerate over the attributes and draw the text using -drawAtPoint method of NSAttributedString+NSStringDrawing (the only iOS 6 specific method). This allow support for custom attributes which can help when tweeking a design.

A Helpful Subclass

Below is a link to a subclass I wrote to work around the problem. It has support for the following…

  • Substring colors
  • Substring fonts
  • Substring baseline offset
  • Substring left offset

With these select attributes you should be able to match the placeholder text to any design.

See the code