banner



How To Add Label Programmatically In Swift 4

adjacent → ← prev

Label

UILabel inherits the UIView grade. It represents a class of views that display one or more lines of read-simply texts. In iOS applications, the label is used in the association with UIControls to fulfill the Application requirements.

The syntax of the UILabel form is given equally follows.

The appearance of the Label can exist configured. We tin can customize the substrings within the label. The label tin show attributed strings, as well. We tin add the labels to the interface programmatically or by using a storyboard. The following steps are used to add together the label to the interface.

  1. Search for the characterization in the Object library and drag the consequence to the storyboard or create an object of UILabel class in the ViewController class.
  2. Configure the appearance of the characterization using the aspect inspector.
  3. Fix auto-layout rules to ascertain the size and position of the label in your interface.

Interface Builder Attributes for UILabel

SN Attribute Clarification
i Text This attribute can be given to set the content of the label. To define the appearance of the label, we tin can assign two modes to the label i.e., Plain mode and attributed style. Plain manner displays the label'southward content with the uniform advent, whereas the attributed style applies styling attributes within the cord. In attributed mode, we can use the More menu to reveal additional appearance attributes. This value can exist accessed at runtime with the text and attributedText backdrop.
two Color It is used to ready the font color of the characterization. In obviously fashion, the color is prepare for the whole content, whereas, in attributed fashion, we can define the color for a particular function of the content.
3 Font This is used to alter the font of the label content. It includes the font family, size, and the opacity of the content. We can define the system or custom font to the characterization. In the custom font, nosotros can fix the font family and style of the label content.
iv Alignment Information technology is used to set the alignment of the label content inside the frame. We tin can place the label content, left marshal, eye, correct-align, justified, or natural within the frame. In patently manner, the alignment is set for the whole characterization content whereas, in attributed style, nosotros can maintain the specific alignment for the specific paragraph of the label.
5 Lines It represents the maximum number of lines the label uses to render the content. We can set information technology to 0 to make the label to return the unlimited lines. This value can be accessed at runtime past using numberOfLines property on the label object.
half-dozen Behavior This attribute is used to control the beliefs of the characterization. The Enabled and highlighted control the appearance of the label. These values can exist accessed at runtime by using isEnabled and isHighlighted holding, respectively.
7 Baseline It is a spacing attribute that controls the vertical alignment of the label when autoshrink is enabled. This value can exist accessed by using the baselineAdjustment holding.
8 LineBreaks It specifies the beliefs of the characterization when the content is too big to adjust within the label bounds. It tin can be set up to word wrap or character wrap to pause the content into multiple lines past words or by characters. This property can be accessed using lineBreakMode property on the label object.
9 Autoshrink This attribute is used to modify the font size of the characterization content before resorting to the truncation. Information technology can be set to either a minimum font scale or maximum font size. Nosotros can choose the minimum font calibration and set the value to allow the label to reduce the font size to fit the text.
10 Highlighted The colour is practical to the text in the characterization when the highlighted attribute is checked.
xi Shadow The default value of this attribute is transparent, which means no shadow appears beneath the text. However, we can mention the shadow color which rendered beneath the text of the label. This value can be accessed at runtime by using shadowColor property.
12 ShadowOffset This property maintains the shadow offset. This value can be accessed using the shadowOffset holding at runtime.

Example

In this case, nosotros will add a characterization to the ViewController using the interface builder, and customize the label appearance at runtime by creating the label outlet in the ViewController class.

To create an outlet, pick the control in the user interface for which the outlet is to be created, hold down the control key and drag from the control to the associated class as shown in the following image.

iOS Label

In ViewController class, we will set some properties for the label to customize its appearance at runtime.

ViewController.swift

Output:

iOS Label

Case two

Making the Label tappable

The following instance volition make the label created, in example 1 tappable. For this purpose, we will create an object of the course UITapGestureRecognizer.

Output:

iOS Label

UILabel Properties

The UILabel object contains the following backdrop, which can be used to customize the runtime beliefs of the characterization.

SN Property Type Description
1 text String It represents the current label text.
ii attributedText NSAttributedString It represents the current styled label text.
3 font UIFont It represents the font of the colour.
4 textColor UIColor It is the label text color.
5 textAlignment NSTextAlignment It is the alignment of the text within the label frame.
six lineBreakMode NSLineBreakMode It is the technique used to wrap and truncate the multiline text
7 isEnabled Bool The enabled state to employ when drawing the label text.
viii adjustFontSizeToFitWidth Bool Information technology is a Boolean value which, when prepare, makes the label font size reduced to fit in the title string.
9 allowsDefaultTighteningForTruncation Bool It is a Boolean value indicating whether the label tightens the text earlier truncating.
x baseLineAdjustment UIBaseLineAdjustment Information technology controls the way baselines are adjusted when the text shrinks to fit in the characterization frame.
11 minimumScaleFactor CGFloat It is the minimum scale factor for the label text.
12 numberOfLines Int It represents the maximum number of lines the label text can have. Gear up it to 0 to make this unlimited.
thirteen highlightedTextColor UIColor Information technology represents the highlight text color practical to the label.
fourteen isHighlighted Bool It represents a Boolean value indicating whether the label text is highlighted or not.
15 shadowColor UIColor Information technology represents the shadow color of the label text.
xvi shadowOffset CGSize It represents the shadow offset of the label text.
17 preferredMaxLayoutWidth CGFloat It is the preferred maximum width for a multiline label.
eighteen isUserInteractionEnabled Bool It is a Boolean type property. When gear up to truthful, it enables the user to interact with the characterization text.

Next Topic iOS Button

← prev next →

How To Add Label Programmatically In Swift 4,

Source: https://www.javatpoint.com/ios-label

Posted by: bryanttretind.blogspot.com

0 Response to "How To Add Label Programmatically In Swift 4"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel