如何在UIlabel中将上标%字符显示为字符串? [英] How to display superscript % character as string in UIlabel?

查看:124
本文介绍了如何在UIlabel中将上标%字符显示为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在UIlabel中将上标%字符显示为字符串?我知道unicode中不存在%作为上标,但是有什么办法可以将%显示为上标而不是使用html标签?

How to display superscript % character as string in UIlabel? I know % does not exist in unicode as a superscript but is there is any way we can display % as a superscript instead of using html tags??

推荐答案

我在Stackoverflow上发现了这篇使用属性字符串的上标样式文本的文章:

I found this post on Stackoverflow on superscript styling text using attributed string:

NSAttributedString上标样式

因此,我使用了该示例:

So using that, I hacked up this demo:

- (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, 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.width, 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];
}

结果:

这篇关于如何在UIlabel中将上标%字符显示为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆