2014-05-05 47 views
0

我试图用这个question来创建一个标题为2的UIButton,但我无法使它工作。我错过了什么?ios7 - 如何将uibutton标题设置为上标?

我的代码几乎是相同的:

if (button.tag == 200) 
     [button setTitle: @"x\u00B3 + x\u00B2 + x\u00B9 + k" forState: UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected]; 

在此先感谢

+1

您发布的代码并没有试图有2²标签。你为什么不简单使用'@“2²”'? – rmaddy

+0

@rmaddy,工作!谢谢! –

回答

2

参考,Unicode的人物造型它将使上下标

Click for Unicode reference

下面是一个例子,

enter image description here

的,

x² - U+00Bx2 -> X\u00B2 
x³ - U+00Bx3 -> X\u00B3 
x⁶ - U+207x6 -> X\u2076 

,等.....

[button setTitle:@"X\u00B2" forState:UIControlStateNormal]; 
[button setTitle:@"X\u00B3" forState:UIControlStateNormal]; 
[button setTitle:@"X\u2076" forState:UIControlStateNormal]; 

这样...

+0

谢谢Vinesh,那也工作得很好(所以rmaddy的笔记,上面) –

+1

,并且是非常有帮助的,再次感谢 –

0

请您在使用地点title此代码:

[NSString stringWithUTF8String:"foot\u00b2"]; 

这是为我工作:

UIBarButtonItem *btnOffer=[[UIBarButtonItem alloc] initWithTitle:[NSString stringWithUTF8String:"foot\u00b2"] style:UIBarButtonItemStylePlain target:self action:@selector(btnOffer_clicked)]; 

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:34.0/255.0f green:182.0/255.0f blue:226.0/255.0f alpha:1.0]; 
[self.navigationItem setRightBarButtonItem:btnOffer]; 
+0

问题是关于设置一个'UIButton'的标题。 – rmaddy

+0

@rmaddy此代码对于UIButton和UIBarButtonItem都有效。因为标题是NSString类型的。 – Rinku

+0

1)为什么使用C字符串而不是普通的旧字符'@“...:'string?2)最好在用户问题的上下文中发布一个答案,设置'UIButton'的标题非常不同于设置'UIBarButtonItem'。 – rmaddy

2

从iOS 6开始,您可以将按钮的标签作为NSAttributedString提供。 kCTSuperscriptAttributeName属性名称设置上标级别(负值也会给出下标)。请注意,您需要导入CoreText/CTStringAttributes.h才能得到它。

#import <CoreText/CTStringAttributes.h> 

// ... 

NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc]initWithString:@"22"]; 

[attributedTitle addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:NSMakeRange(1, 1)]; 

[button setAttributedTitle:attributedTitle forState:UIControlStateNormal]; 
1

让我给你两个方法:

A)使用角色阅读器到标输入到一个NSString字面。

字符查看器通常在菜单栏中的日期旁边。 在搜索框中输入你想要的号码。它将在相关字符下显示上标版本。双击插入。

如果角色阅读器是不存在的,去设置>语言文字&>输入源,并检查“键盘&角色阅读器”

B)使用一个属性串的按钮标签。

这是所有格式化的通用解决方案。这比较困难,但几乎适用于所有方面。

1

我正在使用NSAttributedString最好的结果。

  1. 更改上标文字的字体大小(使用NSFontAttributeName)。
  2. 向上移动上标文字(使用NSBaselineOffsetAttributeName)。

适用于所有类型的字体和所有sub/superscript的值。

//input parameters 
NSString *title = @"e"; 
NSString *superscript = @"x"; 
UIFont *font = [UIFont systemFontOfSize:20.0f]; 

//our buffer 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; 

//just append the title and set its font 
[attributedString appendString:title];   
NSRange titleRange = NSMakeRange(0, title.length); 

[attributedString addAttribute:NSFontAttributeName 
         value:font 
         range:titleRange]; 

//append the superscript 
[attributedString appendString:superscript];  
NSRange superscriptRange = NSMakeRange(title.length, superscript.length); 

//start of the important code - change font and move baseline of the superscript 
[attributedString addAttribute:NSFontAttributeName 
         value:[font fontWithSize:(font.pointSize/2.0f)] 
         range:superscriptRange]; 
[attributedString addAttribute:NSBaselineOffsetAttributeName 
         value:[NSNumber numberWithFloat:(font.ascender/2.0f)] 
         range:superscriptRange]; 
//end of the important code 

[button setAttributedTitle:attributedString forState:UIControlStateNormal]; 

结果:

Superscript screen

+0

任何机会的代码片段?谢谢! –

+0

@DavidDelMonte这是非常详细的如果你不明白这一点,可以学习一些'NSAttributedString'的例子,它会帮你复制粘贴代码 – Sulthan

+0

当我开始编程时,很久以前,我学会了(被教导),最好的程序员是懒惰的,也就是说,如果我们不需要的话,我们不会重塑事情,所以,感谢你的建议,这里有一些回复你:)我的学习风格是这样的,我从复制的代码中学到了很多东西。比任何ot她的方法其实。这就是我在60年代后期学习COBOL的原因。在这里聪明人的帮助下,还有在YouTube上的孩子们让我感到惊讶,我又一次学习了。这很棒。 –