2

Can anyone please check something for me... Just to make sure I'm not going mad!

I created an NSMutableParagraphStyle with tabStops, but they weren't appearing where I was expecting:

float width = self.myLabel.frame.size.width;
self.tabStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width - 50 options:nil],
                            [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width options:nil]];

Then I created a convenience method to create an attributed string with the tab positions for two strings:

- (NSAttributedString *)tabbedTextWithFirstString:(NSString *)firstString secondString:(NSString *)secondString
{
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:kHSTFontFaceDefault size:14.0]};
NSString *tabbedStr = [NSString stringWithFormat:@"\t%@\t", firstString];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:tabbedStr attributes:attributes];

    attributes = @{NSFontAttributeName:[UIFont fontWithName:kHSTFontFaceDefaultBold size:14.0]};
    [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:secondString attributes:attributes]];

    [attrStr addAttribute:NSParagraphStyleAttributeName value:self.tabStyle range:NSMakeRange(0, attrStr.length)];
    return attrStr;
}

This was being applied to two separate UILabels that appear one on top of the other. When I ran the code, inserting the text, the first string looked center aligned, and the second string was being cut off the end of the label:

enter image description here

So then I changed NSTextAlignmentRight for NSTextAlignmentCenter, and now they are right aligned correctly!

enter image description here

I know I have solved my issue, but since there appears to be a mistake in the iOS framework, I don't want the app to break if and when Apple fix this issue. So if anyone could tell me if it is indeed wrong, or if I am actually mistaken, I'd be very grateful, thanks!

jowie
  • 8,028
  • 8
  • 55
  • 94
  • Something I remember that could be causing the issue, try to add `@{NSForegroundAttributeName:[UIColor clearColor]}` at each "right column text" and keep the `NSTextAligmentRight`. – Larme Jun 28 '14 at 10:48
  • Found link: http://stackoverflow.com/questions/16737503/nsmutableattributedstring-add-different-alignments – Larme Jun 28 '14 at 11:01

1 Answers1

0

I just ran into this myself and can confirm that as of Aug. 21, 2015 NSTextAlignmentCenter and NSTextAlignmentRight are swapped.

mputnamtennessee
  • 372
  • 4
  • 14