NSTextAlignmentCenter and NSTextAlignmentRight are the wrong way round in NSTextTab?

白昼怎懂夜的黑 提交于 2019-12-11 03:38:09

问题


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:

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

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!


回答1:


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



来源:https://stackoverflow.com/questions/24434552/nstextalignmentcenter-and-nstextalignmentright-are-the-wrong-way-round-in-nstext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!