索引字符串中的上标美分

MCM*_*tan 3 objective-c nsattributedstring

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

在此输入图像描述

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

在此输入图像描述

我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

如你所见,点后的数字,保持在下面,我想将它们作为第一个例子弹出到顶部.有没有办法设置属性字符串框架?

Lar*_*rme 6

你必须使用NSBaselineOffsetAttributedName.

来自doc:

NSBaselineOffsetAttributeName
此属性的值是一个 NSNumber对象,其中包含一个浮点值,指示字符与基线的偏移量(以磅为单位).默认值为0.适用
于iOS 7.0及更高版本.

从你的例子:

[buyString addAttribute:NSBaselineOffsetAttributeName
                  value:@(10.0)
                  range:NSMakeRange(2, buyString.length - 2)];
Run Code Online (Sandbox Code Playgroud)

您可能需要更改值以满足您的需求.