Superscript cents in an attributed string

╄→尐↘猪︶ㄣ 提交于 2019-12-17 06:56:07

问题


I'm trying to get my label to look like so:

But using attributed string, I managed to get this result:

My code:

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;

As you see, the numbers after the dot, stay below, and I would like to pop them to the top as the first example. Is there any way to set attributed string frame?


回答1:


You have to use NSBaselineOffsetAttributedName.

From the doc:

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.

From your example:

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

You may have to change the value to fit your needs.




回答2:


Why not actually use superscript? You must first #import "CoreText/CoreText.h"

    [buyString addAttribute:(NSString *)kCTSuperscriptAttributeName 
                      value:@1 
                      range:NSMakeRange(2, buyString.length - 2)];


来源:https://stackoverflow.com/questions/25367017/superscript-cents-in-an-attributed-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!