转载自 简书 ~ https://www.jianshu.com/p/89ed22f50a9c
//中间有一点图片上代码截图不全 这里贴上代码
3.字体大小不同文本垂直居中
所以需要设置NSBaselineOffsetAttributeName属性,可以达到目的,注意descender是负值。
//NSBaselineOffsetAttributeName:@(([UIFont systemFontOfSize:30].lineHeight - [UIFont systemFontOfSize:15].lineHeight)/2 + (([UIFont systemFontOfSize:30].descender - [UIFont systemFontOfSize:15].descender))),
NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:15],
NSForegroundColorAttributeName:[UIColor blackColor],
NSBackgroundColorAttributeName:[UIColor yellowColor],
NSBaselineOffsetAttributeName:@(([UIFont systemFontOfSize:30].lineHeight - [UIFont systemFontOfSize:15].lineHeight)/2 + (([UIFont systemFontOfSize:30].descender - [UIFont systemFontOfSize:15].descender))),
NSParagraphStyleAttributeName:ParagraphStyle
};
记录一下
实际测试中发现 NSTextAttachment
无法垂直居中
在stackflow中找到了测试后可行
You can use the capHeight of the font.
Objective-C
NSTextAttachment *icon = [[NSTextAttachment alloc] init];
UIImage *iconImage = [UIImage imageNamed:@"icon.png"];
[icon setBounds:CGRectMake(0, roundf(titleFont.capHeight - iconImage.size.height)/2.f, iconImage.size.width, iconImage.size.height)];
[icon setImage:iconImage];
NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:icon];
[titleText appendAttributedString:iconString];
Swift
let iconImage = UIImage(named: "icon.png")!
var icon = NSTextAttachment()
icon.bounds = CGRect(x: 0, y: (titleFont.capHeight - iconImage.size.height).rounded() / 2, width: iconImage.size.width, height: iconImage.size.height)
icon.image = iconImage
let iconString = NSAttributedString(attachment: icon)
titleText.append(iconString)
The attachment image is rendered on the baseline of the text. And the y axis of it is reversed like the core graphics coordinate system. If you want to move the image upward, set the bounds.origin.y to positive.
The image should be aligned vertically center with the capHeight of the text. So we need to set the bounds.origin.y to (capHeight - imageHeight)/2.
Avoiding some jagged effect on the image, we should round the fraction part of the y. But fonts and images are usually small, even 1px difference makes the image looks like misaligned. So I applied the round function before dividing. It makes the fraction part of the y value to .0 or .5
In your case, the image height is larger than the capHeight of the font. But you can use the same way. The offset y value will be negative. And it will be laid out from the below of the baseline.
翻译:
附件图像呈现在文本的基线上。并且它的y轴像核心图形坐标系一样反转。如果要向上移动图像,请将bounds.origin.y设置为正。
图像应与文本的capHeight垂直居中对齐。因此,我们需要将bounds.origin.y设置为(capHeight-imageHeight)/ 2。
为避免对图像产生锯齿状影响,我们应将y的小数部分四舍五入。但是字体和图像通常很小,即使相差1px,也会使图像看起来像未对齐。所以我在除法之前应用了round函数。它将y值的小数部分设为.0或.5
在您的情况下,图像高度大于字体的capHeight。但是您可以使用相同的方式。偏移y值将为负。它将从基线的下方进行布局。
其中不清楚 capHeight
属性
这里附上资料
NSLog(@"font.pointSize = %f,font.ascender = %f,font.descender = %f,font.capHeight = %f,font.xHeight = %f,font.lineHeight = %f,font.leading = %f",font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);
结果
font.pointSize = 14.000000,font.ascender = 13.330078,font.descender = -3.376953,font.capHeight = 9.864258,font.xHeight = 7.369141,font.lineHeight = 16.707031,font.leading = 0.000000
capHeight
比lineHeight
明显要小很多