如何在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]; }
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。