简体   繁体   中英

How to add indents to Multiple lines of UILabel?

While I found how to add indentations to first line (FirstLineHeadIndent) and to the rest of lines (HeadIndent), I cannot find how to add indents to only first two/three lines in order to achieve something like this: MS Paint万岁!

PS: This is not a duplicate, because I'm not asking how to indent only first line , as one user suggested.

Using TextKit Framework in ios

CGRect checkBoxFrame = [self.textView convertRect:self.checkView.bounds fromView:self.checkView];
checkBoxFrame.origin.x -= self.textView.textContainerInset.left;
checkBoxFrame.origin.y -= self.textView.textContainerInset.top;
UIBezierPath *checkBoxPath = [UIBezierPath bezierPathWithOvalInRect:checkBoxFrame];
self.textView.textContainer.exclusionPaths = @[checkBoxPath];

It will exclude the image path inside content of TextView

You need to set your UILabel text as Attributed string in storyboard.

Then you can edit the indentation of each line, and you can also paste any text you've created with text editor and it will keep its indentation as well as other attributes.

You can of course manipulate these attributes programmatically, here is an example:

@IBOutlet weak var label: UILabel!
 let text = "\tfirst line\n \tsecond line\nthird line\nforth line"

 let paragraphStyle = NSMutableParagraphStyle()
 paragraphStyle.tabStops = [NSTextTab(textAlignment: NSTextAlignment.left, location: 15, options: [:])]
 paragraphStyle.headIndent = 10

 label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])

Here is an example of how to configure it:

在此处输入图片说明

Here is how to configure indentation:

在此处输入图片说明

Here is the example on the simulator:

在此处输入图片说明

You would need to use the stringattribute property of the UILabel.AttributedText.

its actually of type NSMutableAttributedString so I first casted label.AttributedText to the mutable type then I could manipulate it.

To do this you would use the following:

var mutable = Control.AttributedText as NSMutableAttributedString;
UIStringAttributes uiString=new UIStringAttributes();

Then you need to set an indent on the first line (Do this the way you already know how to), and then you would set the headIndent of its paragraph style like below.

this is converted from objective-c so might not be perfect:

NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.headIndent = 14;

NSDictionary attributes (){
    StyleAttributeName = paragraphStyle;
};

mutable.AddAttribute(attributes);

Control.attributedText = mutable;

I believe something like this in combination with your 'FirstLineHeadIndent' code should do the trick.


How to manipulate NSAttributedString

Objective-C second line with indent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM