在NSAttributedString中垂直居中两种不同大小的字体 [英] Center two fonts with different different sizes vertically in an NSAttributedString

查看:856
本文介绍了在NSAttributedString中垂直居中两种不同大小的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSAttributedString 生成两个不同大小的字符串。默认情况下,其底部对齐方式如下所示:

I use NSAttributedString to generate a string with two different sizes. By default, its bottom alignment looks like this:

但我想垂直居中,如下所示:

But I want to center it vertically, like this:

要清楚,这是一个单独的属性字符串,而不是两个或更多。这是一个简单的例子来描述我的问题,我真正喜欢做的事情更复杂。

To be clear, this is a single attributed string, not two or more. This is a simplified example to describe my question, what I'd actually like to do is more complex.

推荐答案

我是说最简单的事情就是操纵相关文本的 NSBaselineOffsetAttributeName 属性:

I'd say the easiest thing to do is just manipulate the NSBaselineOffsetAttributeName attribute for the text in question:


NSBaselineOffsetAttributeName

此属性的值是一个NSNumber对象,其中包含一个浮点值,指示字符的偏移量从基线,点。默认值为0.。

The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.

要居中,您需要考虑大文本的高度与较小文本的高度之间的差异。文本并将其减半,然后将其用作基线调整。

To center, you'd take the difference between height of the large text and the height of the smaller text and halve it, then use that as the baseline adjustment.

这篇关于在NSAttributedString中垂直居中两种不同大小的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆