iOS Vs Android - How To Draw - Part 2 - Styling

Stephen Zaharuk / Tuesday, January 12, 2016

My blog posts until this point have mainly revolved around programming for iOS in Objective-C. There have been some posts around C#, however they're more about switching from C# to Objective-C

However, the past year, i've been splitting my time equally between iOS and and Android development. 

Since more and more mobile app developers are supporting both platforms simultaneously, i figured why not blog about both platforms!

This series of posts will revolve around drawing various things in both platforms. You'll be pleasantly surprised to see that both are actually very similar!

Part 1 - Drawing Intro

Part 2 - Styling

Part 3 - Drawing Shapes 

Part 4 - Drawing Text

Part 2 - Styling

Next, we'll talk about how to set colors, stroke, and other painting type flags for your drawings.

This is where Android and iOS have a slight deviation. Where as in iOS you can set these properties directly on the CGContextRef, in Android you must create a secondary object called Paint, where all of these properties live.

First lets look at a basic one, Background Color


CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);


Paint p = new Paint();

Note, how in the Android snippet, we had to create a paint object. While in the iOS Snippet, we use our context, and call a static method to set the color. 

Now lets look at adding a Stroke, this is a bit more tricky:


CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextSetLineWidth(ctx, 2);


Paint p = new Paint();

in iOS, its just like we did with Background Color, we just the appropriate properties, although instead of it being called stroke width, its called LineWidth.  In Android, though, you should note that the color property we're setting is the SAME property we set for the background color. That means if you want the stroke to be the same color as the background color, that you'll have to draw your path twice, with 2 different paint objects. 

A convenient trick when drawing is to clip your region. For example, you may want to limit what the user sees, but it'd be hard to draw to that limit. SO instead, you draw outside your viewport, but you clip everything outside of it:


CGContextClipToRect(ctx, CGRectMake(0, 0, 100, 100));


 canvas.clipRect(new, 0, 100, 100));

This next part isn't quite styling, but it does affect the out come of the drawing. Transforms. With Transforms, you can rotate or move the point at which you're drawing into in your drawing context. 

In Android and iOS they're both handled very similar:


CGContextTranslateCTM(ctx, x, y);
CGContextRotateCTM(ctx, radians);


canvas.translate(x, y);

in iOS its worth noting that Rotate is in Radians, while in Android its in Degrees!

Finally, in drawing frameworks, you're generally passing around your context (CGContextRef and Canvas), which means that certain styling properties or transforms may be applied to your context, that shouldn't be applied everywhere.  For example, i maybe set a rotate transform for a rectangle that i'm drawing. However, when i go to draw a line later on, i might not want that transform! Does that mean i have to undo the transform? Or is there a shortcut?

Thats where Save/Restore come into play:


// Drawing Code

// Drawing Code

Basically, when you call save on your context, you're saying remember all the settings i had in place before i saved, and when you call restore later, its restores it back to your last save. And save/restore are stack based, meaning you can call save multiple times, and then restore later on: 

For example:

Save() // 1

Save() // 2

Restore() // Restore before 2

Restore() // Restore Before 1

And that covers the basics of styling. 

Stay tuned to learn about drawing!

By Stephen Zaharuk (SteveZ)