position: fixed; top: auto !important; margin-left: 112px;

What is UILabel?

Copied to clipboard..

UILabel is a view which is used to display one or more lines of read-only text. For Example, Email, Password or Bank Name, Branch Name. It can be a single line or multiline.

Important Properties :-

1) .text
2) .font
3) .numberOfLines
4) .lineBreakMode
5) .textColor
6) .backgroundColor
7) .textAlignment

How to create the UILabel Programmatically?

Open Xcode, Goto File > New >Project >Single View Application > Enter Project Name (eg :- FirstProjectViewController) and Select Language as Swift > Done.

In ViewController.swift file, inside viewDidLoad() method view gets initialised only once. Then if in some case you want to reinitialise any UI element then you can use viewWillAppear() method or viewDidAppear() method. These methods are covered in our View Lifecycle article. If in case you have missed those concepts, you can refer them here,

Example :-

This is how we create a UILabel programmatically.


 import UIKit
 var nameLabel = UILabel()
class ViewController: UIViewController {
    
    override func viewDidLoad()
     {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.init(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)

        nameLabel = UILabel(frame:CGRect(x:12 , y:0 , width: self.view.frame.size.width - 12*2 , height: 40) )
        self.view.addSubview(nameLabel)
        nameLabel.textColor = UIColor.black
        nameLabel.text = "Thie is Label"
        nameLabel.font = UIFont .systemFont(ofSize: 18.0, weight: 1.0)
        nameLabel.textAlignment = NSTextAlignment.center
        nameLabel.layer.borderColor = UIColor.black.cgColor
        nameLabel.layer.borderWidth = 1.0
        nameLabel.numberOfLines = 0;
        nameLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
     }
 }

Below Code itself is self explanatory. Firstly, we initialized a "nameLabel" with frame of x as 12, y as 0, width and height. This basically creates a label at the given x,y coordiante of specific given width and height.

Then the label is added to self.view This view is superview of all subViews. In this case, namedLabel is a subView of superview which is self.view.

In next line, we assigned it some text with .text property of Label and Color with .textColor property, Font with .font

numberOfLines is used when you have to print multiline content or description. If you give it a value as 0 then there is no Limitation of content and to break the line .linebreakMode is initialised with .byWordWrapping.

Don't worry for now. You will understand these last two properties in later examples. Just get the basics here, about how to initialise a frame of any UI Element and how to use its Properties like .text,.font,.layer, etc

.layer property

.layer property is used to give borderWidth, borderColor & cornerRadius. To give borderColor along with .layer property you have to give .cgColor at the end as you see in the given example.

Output :-




Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.