ios - 我在我的 View Controller 中尝试将其作为一种方法,但我真的很喜欢在 Label 中使用它

标签 ios uilabel categories nsnumberformatter

我真的很想实现一个像“UILabel+formattedText”这样的类别,它允许我执行一个方法来很好地格式化标签可见显示中的文本,但是任何查看 label.text 的代码只会看到未格式化的数字字符串.我知道这可能很简单。问题是,我似乎找不到它的语法。这究竟是如何运作的?

这是我在 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]]]];           
    }
}

所以我基本上希望至少在标签中加入标签绘制功能。顺便说一下,这段代码是否适用于 MVC?这是我的第一次尝试。

此外,当我在这里时,我可能还会问:这进入了 e 表示法,其数字相对较少,作为 double 。但是,当我尝试将 double 更改为 longlong 等其他内容时,我会得到非常奇怪的结果。我怎样才能获得更高的精度并仍然执行所有这些操作?

最佳答案

我建议编写一个 UILabel 的子类。由于您只想更改文本的显示方式,因此您只需编写一个自定义的 drawTextInRect: 方法。它将使用来自 self.text 的值的格式并绘制结果字符串。然后,您只需更改应格式化的任何标签的类。

例子:

@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

关于ios - 我在我的 View Controller 中尝试将其作为一种方法,但我真的很喜欢在 Label 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560942/

相关文章:

ios - CBCentralManager 可以扩展自定义协议(protocol)吗?我们可以覆盖 CBCentralManagerDelegate 吗?

ios - 在另一个 UIViewController 中从 UiViewController 更改 UILabel 文本

magento - 分层导航过滤器中的子类别

php - 按类别列出项目

ios - 在 View Controller 中使用类别

ios - ImageView 的 removeFromSuperview 和 .hidden 之间的区别

ios - 交付 MDM DeviceLock 负载

iOS 下载 mp3 到应用程序的沙箱

arrays - 将简单代码集成到复杂代码中

ios - 更改语言时在右侧和左侧制作标签