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

余生颓废 提交于 2020-01-01 02:43:11

问题


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.


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/21430685/how-can-i-indent-only-first-line-of-multiline-uilabel-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!