开发者

I tried this as a method in my View Controller, but I'd really like it in a Label

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

开发者_Python百科    }
    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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜