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

标签 ios nsattributedstring

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/

相关文章:

objective-c - NSParagraphStyle 行间距被忽略

swift - 如何在 Swift 中将特定属性添加到 NSAttributedString

html - 如何根据html标签在UILabel中显示html文本?

ios - 当辅助功能打开时,无法打开 UITextView(属性文本)内的 NSURL

ios - 如何获取 UIActivityViewController 的所有 applicationActivities?

ios - 将 UITableView 和截面方向/对齐方式设置为 RTL(从右到左)

ios - 快速代码补全不起作用

ios - 在 ios swift 中自定义 Twitter 数字

ios - 按多个键值对数据进行排序

ios - 需要在 UILabel 的文本中获取多个@符号的 NSRanges 及其后面的文本