objective-c - 属性字符串中的上标美分

标签 objective-c nsattributedstring

我试图让我的标签看起来像这样:

enter image description here

但是使用属性字符串,我设法得到了这个结果:

enter image description here

我的代码:

NSString *string = [NSString stringWithFormat:@"%0.2f",ask];

NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];

[buyString addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:15.0]
                  range:NSMakeRange(2, buyString.length - 2)];

self.labelBuy.attributedText = buyString;

如您所见,点后的数字位于下方,我想将它们弹出到顶部作为第一个示例。 有什么方法可以设置属性字符串框架吗?

最佳答案

你必须使用 NSBaselineOffsetAttributedName

来自文档:

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.
Available in iOS 7.0 and later.

从你的例子:

[buyString addAttribute:NSBaselineOffsetAttributeName
                  value:@(10.0)
                  range:NSMakeRange(2, buyString.length - 2)];

您可能必须更改该值以满足您的需要。

关于objective-c - 属性字符串中的上标美分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367017/

相关文章:

objective-c - 尝试通过 Cocoapods 安装 'Intercom' pod 时 Intercom.bundle 出错

objective-c - 访问在当前 ViewController 的 Storyboard中创建的标签

ios - NSAttributedString 设置字体大小 - swift

ios - NSHTML TextDocument 类型的项目符号点间距和换行起始位置

ios - 在 Swift 4 中设置 textView.typingAttributes 前景色

ios - 如何检测从 html 字符串创建的 NSAttributedString 中的 anchor 标记链接单击

ios - 扩展 NSAttributedString 问题

ios - 减少在 iOS 上使用 OpenCv 的方法消耗的内存

ios - 切换语言后重新加载 tabBarController

objective-c - 直接通过名称绑定(bind) SQLite 参数