Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c - Text indentation

This question is about implementing text indentation ("The placement of text farther to the right to separate it from surrounding text") in iOS.

Take for example the following text:

  1. This is the first section.
  2. This is the second one,
    with two lines.
  3. This is the third.

Notice that the second row in section 2 begin farther to the right and just below the line above.

My code contains an array of NSString, each one should be display as a section with numeric bullet like above. for example:

NSArray *array = [NSArray arrayWithObjects:@"1. This is the first section.", @"2. This is the second one, with two lines.", @"3. This is the third.", nil];

I use UILable to display the text on screen.
To set the text from the array to the label, and to separate each string in a new line I use

myLabel.text = [array componentsJoinedByString:@"\n"];

Any ideas how to get this effect?

like image 785
Eyal Avatar asked Apr 26 '13 16:04

Eyal


People also ask

How to indent css text?

You can use the CSS text-indent property to indent text in any block container, including divs, headings, asides, articles, blockquotes, and list elements. Say you want to indent all div elements containing text on a page to the right by 50px. Then, using the CSS type selector div, set the text-indent property to 50px.

What text-indent?

The text-indent property specifies the indentation of the first line in a text-block. Note: Negative values are allowed. The first line will be indented to the left if the value is negative.

Which property tag styles a paragraph indent using css?

The text-indent property in CSS is used to define the indentation of the first line in each block of text. It also take negative values. It means if the value is negative then the first line will be indented to the left.


1 Answers

This is possible to some degree in iOS6 with - [UILabel setAttributedText:].

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 29;

myLabel.attributedText = [[NSAttributedString alloc] initWithString:
    @"1.\tShort line.\n2.\tLong line with content that triggers wrapping.\n3.\tShort line."
    attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];

Result of the above code

This adds indentation to the subsequent lines. It looks like iOS doesn't support tab stops in the same way as OSX so I'm not seeing a way to adjust the gap between the number and the text. This is probably possible in CoreText.

Of course, you could also just replace the label with a UIWebView and have full formatting control on all versions of iOS at the cost of performance.

like image 177
Brian Nickel Avatar answered Oct 20 '22 00:10

Brian Nickel