Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:25:03+00:00 2026-05-23T14:25:03+00:00

I really want to implement a category like ‘UILabel+formattedText’ that allows me to perform

  • 0

I really want to implement a category like ‘UILabel+formattedText’ that allows me to perform a method which will nicely format the text in the label’s visible display, but any code that looks at label.text just sees the unformatted number string. I know this is probably very simple. Thing is, I can’t seem to find the syntax for that. How would that work exactly?

Here’s my rough draft for the method inside the view controller:

- (void)UpdateLabels{
    if(!formatter){//initialize formatter if there isn't one yet.
        formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
        [formatter setPositiveFormat:@",##0.##"]; 
        [formatter setNegativeFormat:@",##0.##"];
        [formatter setMaximumFractionDigits:15];
        [formatter setMaximumIntegerDigits:15];

    }
    NSRange eNotation =[rawDisplay rangeOfString: @"e"];//if there's an 'e' in the string, it's in eNotation.
    if ([rawDisplay isEqual:@"Error"]) {
        display.text=@"Error";
        backspaceButton.enabled = FALSE;
    }else if (eNotation.length!=0) {
        //if the number is in e-notation, then there's no special formatting necessary.
        display.text=rawDisplay;
        backspaceButton.enabled =FALSE;
    } else {
        backspaceButton.enabled =([rawDisplay isEqual:@"0"])? FALSE: TRUE; //disable backspace when display is "0"            
        //convert the display strings into NSNumbers because NSFormatters want NSNumbers
        // then convert the resulting numbers into pretty formatted strings and stick them onto the labels
        display.text=[NSString stringWithFormat:@"%@", [formatter stringFromNumber: [NSNumber numberWithDouble:[rawDisplay doubleValue]]]];           
    }
}

So I basically want at least the label drawing functionality to go into the label. Is this code being true to MVC by the way? It’s my first try.

Also while I’m here I may as well ask: this goes into e notation with relatively few digits as a double. But when I try to change double to something else such as longlong I get very weird results. How can I have more precision and still do all these operations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:25:03+00:00Added an answer on May 23, 2026 at 2:25 pm

    I would suggest writing a subclass of UILabel. Since all you want to do is change how the text is displayed, you just need to write a custom drawTextInRect: method. It will use a format the value from self.text and draw the resulting string. Then you just need to change the class of any label which should be formatted.

    Example:

    @interface NumericLabel : UILabel {}
    @end
    
    @implementation NumericLabel
    - (void)drawTextInRect:(CGRect)rect {
        static NSNumberFormatter *formatter;
        NSString *text = nil, *rawDisplay = self.text;
    
        if(!formatter){
            //initialize formatter if there isn't one yet.
        }
        NSRange eNotation =[rawDisplay rangeOfString: @"e"];//if there's an 'e' in the string, it's in eNotation.
        if ([rawDisplay isEqual:@"Error"]) {
            text = @"Error";
        }else if (eNotation.length!=0) {
            text = rawDisplay;
        } else {
            text=[formatter stringFromNumber: [NSNumber numberWithDouble:[rawDisplay doubleValue]]];           
        }
    
        [text drawInRect:rect withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really want to write .NET apps that run on any platform (PC, Linux
I really want to put in some sort of section handler into App.config that
I've got an NSColor, and I really want the 32-bit RGBA value that it
Looks like linux doesnt implement pthread_suspend and continue, but I really need em. I
We want to implement chat on our website so that users can communicate with
I really want to create a subclass of FutureTask that has a default no-arg
I have some classes, which have several methods which I don't really want to
It's difficult to explain what I really want. I have an interface which has
I really want to get the google Calendar Api up an running. I found
I really want to be able to have a way to take an app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.