Question

I'm using Text Kit to layout some custom text in an app of mine, and I want to create nice-looking lists, with properly aligned enumerators. I need the text to look like so:

Desired enumeration style

The pink line is not part of the desired effect, but to emphasise how the enumerators should aligned (ie. they need to be right-aligned, but the text on the left should still be left-aligned).

I have the list style implemented bar this requirement (the enumerators are at present left-aligned). I'm doing this by adding the enumerator as a string, with a tab character (\t) after it, and adding a custom tab stop (NSTextTab) at the desired position to the NSParagraphStyle. Subsequent lines are indented by setting the headIndent to the equivalent position as the NSTextTab.

Any thoughts on how I can align the enumerators as I want?

Was it helpful?

Solution

Figured it out. Turns out I can do this just with NSTextTabs, like below:

NSString *enumerator = @"\t\u2022\t"; // Bullet symbol

NSMutableParagraphStyle *enumeratorParagraphStyle = [paragraphStyle mutableCopy];
NSTextTab *enumeratorTabStop = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight
                                                               location:eMarkdownRendererIndentationPoints
                                                                options:nil];
NSTextTab *contentTabStop = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft
                                                            location:contentLocation
                                                             options:nil];

enumeratorParagraphStyle.tabStops = @[enumeratorTabStop, contentTabStop];
[textStorage appendAttributedString:[[NSAttributedString alloc] initWithString:enumerator
                                                                    attributes:@{NSParagraphStyleAttributeName: enumeratorParagraphStyle}]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top