2014-05-05 46 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上的孩子們讓我感到驚訝,我又一次學習了。這很棒。 –