我试图让我的标签看起来像这样:
但是使用属性字符串,我设法得到了这个结果:
我的代码:
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 anNSNumber
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/