iOS为什么设置NSBaselineOffsetAttributeName时NSTextAttachment会消失?

我想将NSTextAttachment与文本的中心对齐,所以我设置了
NSBaselineOffsetAttributeName用于更改NSTextAttachment的基线.
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]}];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"micon"];        
NSMutableAttributedString *ats = [[NSAttributedString attributedStringWithAttachment:attachment] mutablecopy];
[ats addAttributes:@{NSBaselineOffsetAttributeName:@(-5),NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]} range:(NSRange){0,ats.length}];
[attrString appendAttributedString:s];

然后我计算UILabel的大小并设置attributedText.

CGRect textFrame = [attrString boundingRectWithSize:size options:NsstringDrawingUsesLineFragmentOrigin|NsstringDrawingUsesFontLeading context:nil];
UILabel *label = [[UILabel alloc] initWithFrame:textFrame];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
label.attributedText = attributed;
label.backgroundColor = [UIColor clearColor];

最后,最后一张图片消失了.

任何人都可以解释为什么会发生这种情况以及如何解决它.

解决方法

我最后通过添加一个属性NSParagraphStyleAttributeName来解决
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = lineheight; //line height equals to image height
    [attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];

不要改变图像的偏移量,只需设置文本的偏移量.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...