1

UIKit.h を使用して iPad プロジェクトで数値をフォーマットするために次のコード スニペットを使用していますが、負の数値を赤で表示する方法がわかりません。ドキュメントに基づいて私が試したことは、うまくいきません。これに関するいくつかの提案をいただければ幸いです。

 NSNumberFormatter *decimalStyle = [[[NSNumberFormatter alloc] init] autorelease];
[decimalStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[decimalStyle setNumberStyle:NSNumberFormatterDecimalStyle];
[decimalStyle setPositiveFormat:@"###,###,##0"];
[decimalStyle setNegativeFormat:@"(###,##0)"];
4

3 に答える 3

4

現時点では、直接それを行うことはできません。できることは次のようなものです。

UILabel *numberLabel = ...; // The label that will display your number
numberLabel.text = [decimalStyle stringFromNumber:myNumber];
numberLable.textColor = (myNumber.floatValue < 0.0f) ? [UIColor redColor] : [UIColor blackColor];
于 2012-07-09T17:37:55.987 に答える
2

これがiOSで機能するかどうかはわかりませんが、OSXIでは次のようにします。

//create new instance of NSNumberFormatter
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

//create mutable dictionary for storing formatting attributes
NSMutableDictionary *negativeNumberColor = [NSMutableDictionary dictionary];

//set red colour
[negativeNumberColor setObject:[NSColor redColor] forKey:@"NSColor"];

//apply red colour to number formatter
[numberFormatter setTextAttributesForNegativeValues: negativeNumberColor];

//set formatter on NSTable column cell
[[tableColumn dataCell] setFormatter: numberFormatter];
于 2012-11-01T21:36:12.777 に答える
1

答えは、テキストをどのように描画しているかによって異なりますが、アイデアは、テキストを好きな色で描画することです。たとえば、UILabel で数字を描画している場合textColor、ラベルの を必要に応じて赤または黒に設定できます。Core Text を使用してテキストを描画している場合は、NSAttributedString を使用して、問題のテキストに色属性を追加できます。テキストを HTML でレンダリングする場合は、もちろん、そのテキストを囲む HTML タグの適切な属性を設定する必要があります。

于 2012-07-09T17:38:19.003 に答える