NSMutableAttributedString * attrString =({
//单词
NSAttributedString * leftString = [[NSAttributedString alloc] initWithString:@“好”属性:@ {NSFontAttributeName:[UIFont systemFontOfSize:10]}]];
// 图片
NSAttributedString * attachString =({
UIImage * image = [UIImage imageNamed:@“项目标签”];
NSTextAttachment *附加= [NSTextAttachment新];
attach.image =图片;
[NSAttributedString attributedStringWithAttachment:attach];
});
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithAttributedString:leftString];
[attrString appendAttributedString:attachString];
//设置行高
NSMutableParagraphStyle * style = [NSMutableParagraphStyle new];
style.maximumLineHeight = 22.5;
style.minimumLineHeight = 22.5;
[attrString addAttribute:NSParagraphStyleAttributeName值:样式范围:NSMakeRange(0,attrString.length)];
//设置baselineOffset
[attrString addAttribute:NSBaselineOffsetAttributeName值:@ 0范围:NSMakeRange(0,attrString.length)];
attrString;
});
如果图像附件之前有一些单词,则图像的attributedString的基线偏移量不能设置为0。
here is the demo project
最佳答案
我自己解决了。
只需将NSTextAttachment的bound.origin.y设置为字体的后代即可。
关于ios - iOS NSTextAttachment无法将基线偏移量设置为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165149/