ios – 如何在UIlabel中显示上标%字符作为字符串?

前端之家收集整理的这篇文章主要介绍了ios – 如何在UIlabel中显示上标%字符作为字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在UIlabel中显示上标%字符作为字符串?我知道%在unicode中不存在上标,但是有什么办法可以显示%作为上标而不是使用html标签

解决方法

我发现这个帖子在Stackoverflow上使用归因字符串的上标造型文本:

NSAttributedString superscript styling

所以使用这个,我搞砸了这个演示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:20];

    UILabel *textBlock1 = [[UILabel alloc] initWithFrame:CGRectMake(0,self.view.bounds.size.width,self.view.bounds.size.height / 2.0)];
    textBlock1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    textBlock1.textAlignment = NSTextAlignmentCenter;
    textBlock1.font = font;

    textBlock1.text = @"57%";





    UILabel *textBlock2 = [[UILabel alloc] initWithFrame:CGRectMake(0,self.view.bounds.size.height / 2.0,self.view.bounds.size.height / 2.0)];
    textBlock2.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    textBlock2.textAlignment = NSTextAlignmentCenter;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"57%"
                                                                                         attributes:@{NSFontAttributeName: font}];
    [attributedString setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:10],NSBaselineOffsetAttributeName : @10} range:NSMakeRange(2,1)];

    textBlock2.attributedText = attributedString;



    [self.view addSubview:textBlock1];
    [self.view addSubview:textBlock2];
}

结果:

猜你在找的iOS相关文章