Make your custom view’s properties available in Storyboards

Let’s say you want to configure some parts of your view rendering directly in your storyboards.  Fortunately you can by using @IBInspectable.

Syntax

Result in Xcode

Screen Shot 2016-06-14 at 3.34.17 PM

Simulator

Screen Shot 2016-06-14 at 3.34.13 PM

Here is the project ( Xcode 8, Swift 3) if you want to try it : IBInspectableDemo

That’s it folks, have a good day !

MAB

 

How to outline your UILabel & UIButton

I recently had a problem with a yellow label on a dynamic background that can sometimes also be yellow.  The two common solutions are

  • Add a shadow.
  • Outline your text.

NSAttributedString offers everything you need to do it.

Filled & Outlined

Screen Shot 2016-06-09 at 1.32.07 PM

 

Filled ?

Note that the stroke width attribute is a negative value.

This is to tell the drawing engine it is a “Stroke & Fill” operation.  Otherwise it’ll just ignore the “NSForegroundColorAttributeName” and leave the text transparent like this :

Screen Shot 2016-06-09 at 1.44.33 PM

Link to the playground : MABDev.OutlinedText.playground

Documentation : https://developer.apple.com/library/mac/qa/qa1531/_index.html

Cheers,

MAB

The swift class extension you’ll want to include in all your projects

Here is one of my favorite extension I created for Array. Very useful shortcut when you use abstraction ( as you should ) and you want to create an array from another one.

Description

Instantiate an Array of type A with an another array of type ( or protocol ) B and fills it with elements from B that can be cast to A.

Code

Example

Screen Shot 2016-06-08 at 2.57.33 PM

You could always use .filter but it would take way more lines.  I often use it to get a specific subview in my view hierarchy.  Here’s the link to the playground file.

MABDev.ArrayExtension.playground

That’s it for today folks !  See you soon. MAB.

 

UIImage, prepareForReuse and multiple reload data

So you are working on a UICollectionView and you have to call “reloadData()” a few times in a row because you have data coming in. I know it would be better to insert items or at least reload the section but sometimes for good or bad reasons, like custom layout manager and iOS 9, it’s not always possible. And it’s not the point here anyway.  The point is I recently had a problem with my UIImageView flashing like crazy overtime I call “reloadData()” .  I found out in your “prepareForReuse()” implementation, rather than setting you imageView.image = nil you’re better off doing imageView.hidden = true and unhide it later when you need to set it again in “cellForItemAtIndexPath”.

MyCell.swift

Not much but I thought it could interest some of you guys. Cheers !

Edit: It might not be the best solution after all. It behave poorly in bad / slow network conditions. 

Hello World

Hello word !  First blog post ever.  Never heard of me huh ?  No worries, so does 99.99% of the population.  We’ll get to know each other I’m pretty sure of it.  All you need to know for now is that this blog is about my new company “MAB Développement Mobile inc.” but most importantly about iOS and Swift.  I want to use this place as a repository of all those priceless snippets of code and good tricks you get to learn but too often lose and forget.  It’s also a forum so I’m counting on you to put your shoulder to the wheel.  I also want to have fun doing it and reading your comments, because that’s what life is about… fun.