Patrick Perini
Patrick Perini

Reputation: 22633

NSAttributedString / NSTextTab Alignment Issue

GOAL:

I'm attempting to use NSAttributedStrings (in conjunction with NSTextTabs) to create the following layout:

[ Title           # ]  <-- Useable in NSTableViews, NSMenuItems, etc.
[ Another Title   # ]
[ T3              # ]

ATTEMPTED SOLUTION:

The code I'm attempting is:

NSMutableParagraphStyle *tabStyle = [[NSMutableParagraphStyle alloc] init];
[tabStyle setTabStops: [NSArray array]];
[tabStyle addTabStop: [[NSTextTab alloc] initWithType: NSRightTabStopType location: 200.0]];
[attrString appendAttributedString: [[NSMutableAttributedString alloc] initWithString: @"\t"]];
[attrString addAttribute: NSParagraphStyleAttributeName value: tabStyle range: NSMakeRange(0, [attrString length])];
[attrString appendAttributedString: [[NSMutableAttributedString alloc] initWithString: @"1"]];

Where attrString is an NSMutableAttributeString, currently set to the "Title".

However, using this code (which I would assume would produce the desired output), produces the following:

Screenshot - NSTextTabs

FURTHER INFORMATION:

When I remove the references to NSTextTabs, like so:

[attrString appendAttributedString: [[NSMutableAttributedString alloc] initWithString: @"\t"]];
[attrString appendAttributedString: [[NSMutableAttributedString alloc] initWithString: @"1"]];

I get the expected output of uneven tabbing.

Screenshot - No NSTexTabs

BOTTOM LINE:

Why is the NSAttributedString seemingly ignoring the NSParagraphStyle/NSTextTabs?

What can I do to fix this?

Upvotes: 2

Views: 1210

Answers (1)

Patrick Perini
Patrick Perini

Reputation: 22633

Found the issue, by making an NSTextView in IB and placing the AttributedString into it.

enter image description here

Apparently, the layout needs to be "Scrolls" (was "Truncates") in order to produce the desired effect.

Upvotes: 2

Related Questions