gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:12:29 25 4
gpt4 key购买 nike

如何在 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, 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];
}

结果:

enter image description here

关于ios - 如何在 UIlabel 中将上标 % 字符显示为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com