Objective c - Text indentation
Asked Answered
C

3

10

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?

Chuvash answered 26/4, 2013 at 16:18 Comment(3)
u need to override the drawrect (for the control) and need to write your own indentation code to solve this issue.Interlocution
Are you trying to do it in a bulleted list like shown? How are you rendering the overall block? Can you give more context?Bronk
@BrianNickel yes i need the bullet style, I added information to my question.Chuvash
C
2

Well I decided to implement it my self without using Core Text, I just created a view strcture that make all the indentation work by itself, and let you customize it as you want.

For all of you interested in the implementation, you can see the source code and an example project here:
ECListView Project

Chuvash answered 27/4, 2013 at 22:58 Comment(0)
B
26

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.

Bronk answered 26/4, 2013 at 19:46 Comment(0)
C
2

Well I decided to implement it my self without using Core Text, I just created a view strcture that make all the indentation work by itself, and let you customize it as you want.

For all of you interested in the implementation, you can see the source code and an example project here:
ECListView Project

Chuvash answered 27/4, 2013 at 22:58 Comment(0)
L
0

UILabel is not going to cut it if you have any kind of specific layout requirements. For that, you're going to need to dig into Core Text. The good news is that Core Text will let you do any kind of text layout you can imagine. The bad news is that all that power brings with it some complexity, so to use it you're going to have to invest some time learning how the framework works.

An alternative that's suitable in some situations is to use a web view to display your text. UIWebView will let you do whatever text layout you can manage using HTML and CSS.

Levania answered 26/4, 2013 at 16:46 Comment(1)
10x for your reply, I imagined I'll need to use the Core Text framework.. This is very generic issue, I suppose a lot of people already counter this problem and implemented some generic solution. So I rather not invent the wheel again but to get some reference to some code snippet or tutorial.Chuvash

© 2022 - 2024 — McMap. All rights reserved.