NSTextAlignmentCenter and NSTextAlignmentRight are the wrong way round in NSTextTab?
Asked Answered
H

1

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!

Hilary answered 26/6, 2014 at 15:29 Comment(2)
Something I remember that could be causing the issue, try to add @{NSForegroundAttributeName:[UIColor clearColor]} at each "right column text" and keep the NSTextAligmentRight.Gurge
Found link: #16738003Gurge
S
0

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

Sextodecimo answered 21/8, 2015 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.