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

What is UILabel?

UILabel is used to display the static Contents. For Example, "Email", "Password" or anything. It can be a single line or multiline.

Important Properties :-

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

So Lets Start Creating UILabel

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

Now you can see a file on left navigation Menu of Xcode named, ViewController.swift

Grab the concepts of UILabel in iOS here in this Example.

Example :-

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".Then added it to self.view and assigned it some text and given textColor with "UIColor" class and font with "UIFont" class.

numberOfLines is used to give how much content you want for the label to adjust. If you give it 0 value that means there is no Limitation of content if assigned with lineBreaking mode. You will get these last two properties in later examples. Just get the basics here, that is how to initialize a frame to any UIElement, 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 :-


Solve the Quiz of Article

1) What do you think the frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within?
Yes
No

2) What do you think the bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0)?
Yes
No


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.