我怎样才能在iOS中缩进多行UILabel的第一行?

时间:2022-09-06 21:08:24

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.

我想在UILabel的开头添加一个图像。标签是多行的。如果我使用contentInset,它会缩进整个标签,但我只想缩进第一行。

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.

它看起来应该是这样的。

我怎样才能在iOS中缩进多行UILabel的第一行?

2 个解决方案

#1


12  

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

@DavidCaunt的建议对我有用。我在这里分享代码。

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

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

[valueLabel setAttributedText:attributedText];

#2


1  

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

另外,作为user716216指出 - 我们可以使用具有定义的缩进值的选项卡:

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


12  

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

@DavidCaunt的建议对我有用。我在这里分享代码。

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

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

[valueLabel setAttributedText:attributedText];

#2


1  

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

另外,作为user716216指出 - 我们可以使用具有定义的缩进值的选项卡:

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}];