Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I indent only first line of multiline UILabel in iOS?

I want to add an image in start of UILabel. Label is multiline. If I use contentInset, it indent the whole label but I want to indent first line only.

I have tried this so far, this doesn't work for me.

    UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0, 40.0, 0.0, 0.0);
    valueLabel.contentInset = titleInsets;

It should look like this.

enter image description here

like image 961
GMJigar Avatar asked Jan 29 '14 12:01

GMJigar


People also ask

How do you control line spacing in UILabel?

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels." This is a really old answer, and other have already addded the new and better way to handle this..

How do I change my UILabel text?

Changing the text of an existing UILabel can be done by accessing and modifying the text property of the UILabel . This can be done directly using String literals or indirectly using variables.

What is a ui label?

label. A non-editable text control. The component is used to display static text on a page.

What is label swift?

Label is a user interface item in SwiftUI which enables you to display a combination of an image (icon, SF Symbol or other) and a text label in a single UI element.


2 Answers

Here's how you can do this in Interface Builder:

Demonstrates how to indent the first line of a label in Interface Builder

like image 157
Scott Gardner Avatar answered Sep 27 '22 23:09

Scott Gardner


@DavidCaunt suggestion worked for me. I am sharing code here.

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 50;

[attributedText addAttribute:NSParagraphStyleAttributeName value:style range:range];

[valueLabel setAttributedText:attributedText];
like image 30
GMJigar Avatar answered Sep 27 '22 21:09

GMJigar