ios - 在 NSAttributedString 中垂直居中两种不同大小的字体

标签 ios nsattributedstring

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

baseline aligned sizes

但我想将它垂直居中,像这样: vertically centered sizes

要清楚,这是一个单一的属性字符串,而不是两个或更多。这是描述我的问题的简化示例,我实际上想做的更复杂。

最佳答案

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

NSBaselineOffsetAttributeName

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.

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

关于ios - 在 NSAttributedString 中垂直居中两种不同大小的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19487369/

相关文章:

swift - NSMutableAttributedString 无法正确呈现

iphone - 如何知道当前设备是否可以有后台模式

ios - iOS中使用performSelector的回调函数

ios - 如何在 iOS 的属性按钮中设置颜色?

ios - 从 NSXMLElement iOS sdk 获取 xmlns 的值

ios - 如何从一侧弯曲或扭曲 UILabel 文本

iphone - xcode 中的工具栏和后退按钮问题

ios - 在 watchOS2 中在 Watch 应用程序和 iOS 应用程序之间共享数据

ios - Xcode 11,使用 Main.storyboard 修复的主界面

ios - UITextView/UILabel Swift 中每个链接的不同文本属性