简体   繁体   English

目标C中的自定义NSFormatter

[英]Custom NSFormatter in Objective C

I am trying to write my own custom formatter in Objective C by subclassing NSNumberFormatter. 我试图通过子类化NSNumberFormatter在Objective C中编写自己的自定义格式化程序。 Specifically what I'd like to do is make a number turn red if it is above or below certain values. 具体来说,我想做的是使某个数字高于或低于某些值时将其变成红色。 The apple documentation says 苹果文件

For example, if you want negative financial amounts to appear in red, you have this method return a string with an attribute of red text. 例如,如果要使负财务金额显示为红色,则可以让此方法返回带有红色文本属性的字符串。 In attributedStringForObjectValue:withDefaultAttributes: get the non-attributed string by invoking stringForObjectValue: and then apply the proper attributes to that string. 在attributedStringForObjectValue:withDefaultAttributes中:通过调用stringForObjectValue:获得未归因的字符串,然后将适当的属性应用于该字符串。

Based on this advice I implemented the following code 基于此建议,我实现了以下代码

- (NSAttributedString*) attributedStringForObjectValue: (id)anObject withDefaultAttributes: (NSDictionary*)attr;
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[self stringForObjectValue:anObject]];

    if ([[attrString string] floatValue] < -20.0f) {
        [attrString addAttribute:@"NSForegroundColorAttributeName" value:[NSColor redColor] range:NSMakeRange(0, 10)];
        return attrString;
    } else return attrString;
}

But when I test this all it does is freeze my application. 但是,当我测试所有这些时,它只会冻结我的应用程序。 Any advice would be appreciated. 任何意见,将不胜感激。 Thanks. 谢谢。

I believe this has something to do with your NSRange that you create. 我相信这与您创建的NSRange有关。 I believe your length (10 in your example) is out of bounds. 我相信您的长度(在您的示例中为10)超出范围。 Try getting the length of the string that you use to initialize your NSMutableAttributedString . 尝试获取用于初始化NSMutableAttributedString的字符串的长度。

For example: 例如:

- (NSAttributedString*) attributedStringForObjectValue: (id)anObject withDefaultAttributes: (NSDictionary*)attr;
{
    NSString *string = [self stringForObjectValue:anObject];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSInteger stringLength = [string length];

    if ([[attrString string] floatValue] < -20.0f)
    {
        [attrString addAttribute:@"NSForegroundColorAttributeName" value:[NSColor redColor] range:NSMakeRange(0, stringLength)];
    }

    return attrString;
}

Here is how I was finally able to implement this. 这就是我最终能够实现此目标的方式。 To make it more visible when a number is negative, I decided to make the background of the text red with white text. 为了使它在数字为负数时更加可见,我决定将文本的背景设置为红色并带有白色文本。 The following code does work in a NSTextField cell. 以下代码在NSTextField单元中确实起作用。 I'm not sure why the code in my question (and the answer) does not work, addAttribute should work. 我不确定为什么我的问题(和答案)中的代码不起作用,addAttribute应该起作用。

- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:  (NSDictionary *)attributes{

    NSString *string = [self stringForObjectValue:anObject];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSInteger stringLength = [string length];

    if ([[attrString string] floatValue] < 0)
    {
         NSDictionary *firstAttributes = @{NSForegroundColorAttributeName: [NSColor whiteColor],
                                      NSBackgroundColorAttributeName: [NSColor blueColor]};
    [attrString setAttributes:firstAttributes range:NSMakeRange(0, stringLength)];
}

return attrString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM