Beginner’s guide to Enum in Swift

Swift version of Enum is much more powerful

What is Enum

Enum is a data structure that can only has discrete values.

It is always used if you need to store a group of related values like directions on map or compass(North, East, West, South), status of download process (Completed, Cancelled, Paused, Downloading) and so on. Continue reading “Beginner’s guide to Enum in Swift”

iOS developer’s guide to NSAttributedString in Swift 3

Customize every single character in your string

What is NSAttributedString

It is an object that manages string and associated set of attributes applied to individual characters or range of characters.

It is a class belongs to the Foundation framework and it is NOT a subclass of NSString. Instead it contains NSString object that it applies attributes to.

The cluster has two public class NSAttributedString and NSMutableAttributedString Continue reading “iOS developer’s guide to NSAttributedString in Swift 3”

How to add custom font to iOS app in Xcode

You can add True Type Font (.ttf) or Open Type Font (.otf) to your app and use it the same way you use font provided by iOS.

Add your font file to your Xcode project

From menu bar select File->Add Files to “project_name” or just drag font files to your project Continue reading “How to add custom font to iOS app in Xcode”

Custom controls using @IBDesignable and @IBInsepectable

You can design custom controls and render them in real time from Interface Builder using @IBDesignable and @IBInspectable attributes.

In the following example we will create a custom UIButton and change its corner radius, border color and border width from InterFace Builder with the help of @IBDesignable and @IBInspectable attributes. Continue reading “Custom controls using @IBDesignable and @IBInsepectable”

How to change UILabel height dynamically to fit all text

In two steps we can make a UILabel change its height dynamically without writing code.

UILabel dynamic height

First select your UILabel and from “Identity inspector” set number of lines property to 0.

UILabel dynamic height

Next add Top, Trailing, Leading and Height constraints then select the height constraint and change its “Relation” property from “Equal” to “Greater Than or Equal” Continue reading “How to change UILabel height dynamically to fit all text”

Working with Dates in Swift

Working with Dates in Swift

To start working with dates in iOS you must be familiar with at least these three objects: Date, DateFormatter and DateComponents.

Date struct

Date is a struct that belongs to the Foundation framework.

It represents a specific point in time independent of any calendar or time zone.

Date structure bridges to the NSDate class.

It provides methods for creating dates, comparing dates and calculating time intervals between dates. Continue reading “Working with Dates in Swift”

Remove empty rows at the end of UITableView

Removing Empty Rows from the end of UITableView

Sometimes you have a table view with very few number of rows that are not covering the whole screen height.

In that case Xcode will add extra empty rows to your table view which looks very unprofessional. Continue reading “Remove empty rows at the end of UITableView”

Custom header view from xib for UITableView

In this step by step tutorial we will create a custom header view to use in UITableView and it will be loaded from xib file.

Creating the UITableViewHeaderFooterView class and xib

1- First of all create a new Swift file and make it subclass of UITableViewHeaderFooterView and name it “DemoHeaderView” Continue reading “Custom header view from xib for UITableView”