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

3

12

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

Felipa answered 29/1, 2014 at 12:8 Comment(0)
F
16

@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];
Felipa answered 29/1, 2014 at 13:45 Comment(1)
I know this is the right approach, but did you try it with custom fonts. For me it doesnt work.Hibiscus
S
1

As a user716216 pointed, additionally - we can use a tab with defined indent value:

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.headIndent = 50;

label.attributedText = [[NSAttributedString alloc] initWithString:
    @"\tHow can i add image like this in start of UILabel? Label is multiline.........."
    attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
Salimeter answered 29/1, 2014 at 12:45 Comment(1)
I had to indent 2 rows in one UILabel so I used: paragraphStyle.firstLineHeadIndent = 7; paragraphStyle.headIndent = 7;Thought
P
1

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

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

Publia answered 20/12, 2018 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.