Domanda

I have tried to use this question to create a UIButton that has a title of 2 squared.But I can't get it to work. What am I missing?

My code is virtually identical:

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

Thanks in advance

È stato utile?

Soluzione

Refere Unicode Charactor it will make subscripts and superscripts

Click for Unicode reference

Here is an example,

enter image description here

for,

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

, etc.....

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

like that...

Altri suggerimenti

As of iOS 6, you can supply a button's label as anNSAttributedString. The kCTSuperscriptAttributeName attribute name sets the superscript level (negative values give subscripts as well). Note that you need to import CoreText/CTStringAttributes.h to get it.

#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];

Let me give you two approaches:

A) Use the character viewer to type in the superscript into an NSString literal.

The character viewers is normally next to the date in the menu bar. Type the number you want into the search box. It will display under related characters the superscript version. Double click on that to insert.

If character viewer is not there, go to settings>Languages & Text>Input Sources and check "keyboard & character viewer"

B) Use an attributed string for the button label.

This is the general purpose solution for all formatting. It's more difficult but works for nearly everything.

I am getting the best results using a NSAttributedString.

  1. Change font size for superscript text (using NSFontAttributeName).
  2. Move superscript text up (using NSBaselineOffsetAttributeName).

Works for all types of fonts and all values of 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];

Result:

Superscript screen

Could you please use this code in place of title:

[NSString stringWithUTF8String:"foot\u00b2"];

This is working for me:

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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top