ios NSTextAttachment 无法将基线偏移设置为零

ios NSTextAttachment can not set baseline offset to zero

NSMutableAttributedString *attrString = ({

    // words
    NSAttributedString *leftString = [[NSAttributedString alloc] initWithString:@"好" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]}];

    // image
    NSAttributedString *attachString = ({
        UIImage *image = [UIImage imageNamed:@"项目标签"];
        NSTextAttachment *attach = [NSTextAttachment new];
        attach.image = image;

        [NSAttributedString attributedStringWithAttachment:attach];
    });

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithAttributedString:leftString];
    [attrString appendAttributedString:attachString];

    // set line-height
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.maximumLineHeight = 22.5;
    style.minimumLineHeight = 22.5;

    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrString.length)];

    // set baselineOffset
    [attrString addAttribute:NSBaselineOffsetAttributeName value:@0 range:NSMakeRange(0, attrString.length)];

    attrString;
});

如果图片附件前有文字,图片的attributedString的baselineOffset不能设置为0

here is the demo project

我自己解决了

只需将 NSTextAttachment bound.origin.y 设置为字体降序。