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

3 Answers 3

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]; 
Sign up to request clarification or add additional context in comments.

1 Comment

I know this is the right approach, but did you try it with custom fonts. For me it doesnt work.
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}]; 

1 Comment

I had to indent 2 rows in one UILabel so I used: paragraphStyle.firstLineHeadIndent = 7; paragraphStyle.headIndent = 7;
1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.