标签: nstexttab

NSAttributedString带有图像附件和NSTextTab,文本未对齐

我正试图在左边有一个带有图像和标题的UIlabel,在右边有一个带有项目符号的描述列表,为此我正在使用NSAttributedString,如下所示:

        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;
Run Code Online (Sandbox Code Playgroud)

我希望右边的列表保持对齐但事实并非如此,这是我得到的结果:

在此输入图像描述

我期望列表是这样对齐的: 在此输入图像描述

nstextattachment core-text ios nstexttab nsparagraphstyle

8
推荐指数
1
解决办法
2037
查看次数