Color Literals & Image Literals Tutorial in Swift - iOS - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

19 Sept 2017

Color Literals & Image Literals Tutorial in Swift - iOS

Swift supports Both Color Literals & Image Literals. In this we will see how to use these literals.

This Article Covers Following Related questions  :

how to add image literal to an array and show them?
swift #imageliteral
color literals xcode 8
swift #colorliteral
image literal swift 3
xcode 8 image literal not working
color literal swift
imageliteral swift 3
#imageliteral(resourcename:"image-name")


Color Literals & Image Literals.

Color Literals :

Normally we write colors as follow :
let color = UIColor(
    red     : 200.0/255.0,
    green   : 200.0/255.0, 
    blue    : 200.0/255.0,
    alpha   : 1.0
)

That's too long for writing and time taking one.

We can do easily by using Color Literal.

The following image will show how to add color using Color Literals.

Color Literals example


On Start typing Color Literals we will see list of all colors as above then we can select easily by visualizing.

This is of type UIColor.

Whenever you want to select another color, just double-click inside any of those rects and the color picker will open again.

Like that we can store in arrays also.
Color Literals Array example

Image Literals :

Image Literals are as same as color literals. when ever we start typing Image Literals we will see list of images that are present in your project. That's really cool.

The following image will show how to add color using Color Literals.

Image Literals Example


This is of type UIImage.

Whenever you want to change image, just double-click inside any of those rects and the image picker will open again.

Like that we can store in arrays also.

Image Literals Array Example

Conclusion :

Color and image literals are really great. It reduces verbosity in the code and are much more visual.

4 comments:

  1. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Latest technologies

    ReplyDelete
  2. How do we refer these image assets if we are using framework...?

    ReplyDelete
  3. It makes it hard to verify that the correct color or image is used, the name (or values in the case of an RGB color spec) should appear somewhere.

    ReplyDelete

Post Top Ad