ioscore-textnstextattachmentnsparagraphstylenstexttab

NSAttributedString with image attachment and NSTextTab, text not aligned


I'm trying to have a UIlabel with an image and title on the left and a list of descriptions with bullets on the right.

To do that I'm using NSAttributedString like this :

NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.tabStops =
    @[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabLocation options:[NSDictionary dictionary]] ];

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"test_image"];
textAttachment.bounds = CGRectMake(0, -3, 15, 15);//resize the image
attString = [NSAttributedString attributedStringWithAttachment:textAttachment].mutableCopy;
[attString appendAttributedString:[[NSAttributedString alloc]
                                      initWithString:[NSString stringWithFormat:@"title\t\u2022 %@",
                                                                                [@[ @"description1", @"description2" ]
                                                                                    componentsJoinedByString:@"\n\t\u2022 "]]
                                          attributes:@{NSParagraphStyleAttributeName : pStyle}]];
label.attributedText = attString;

I expect the list on the right to be left aligned but that's not the case, here is the result I get:

enter image description here

What I expect is the list to be aligned like this:

enter image description here


Solution

  • The issue is with location parameter in NSTextTab

    RESULT:

    enter image description here