Xuni コントロール > FlexGrid > 機能 > 列の書式設定 |
FlexGrid では、任意の数の列に独自のカスタム NSNumberFormatter または NSDateFormatter を提供できます。書式設定は、セルの表示値にのみ適用され、基底のデータには影響しません。数値列または日付列の書式設定を行うには、フォーマッタのインスタンスを列の formatter プロパティに割り当てます。
数値列または日付列の書式設定を行うには、フォーマッタのインスタンスを列の formatter プロパティに割り当てます。
デフォルトでは、日付列は「3/31/2016」のような短い日付書式で表示されます。次のコードは、NSDateFormatter を使用して、FlexGrid の列にカスタム日付書式を適用します。
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = “M-dd h:mm a”
dateFormatter.locale = NSLocale.currentLocale()
let col = flexGrid.columns.objectAtIndex(0)
col.formatter = dateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M-dd h:mm a"];
GridColumn *col = [flexGrid.columns objectAtIndex:0];
col.formatter = dateFormatter;
NSDateFormatter dateFormatter = new NSDateFormatter();
dateFormatter.DateFormat = “M-dd h:mm a”;
dateFormatter.Locale = NSLocale.CurrentLocale;
Column col = flexGrid.Columns.GetItem<Column>(0);
col.Formatter = dateFormatter;
DateFormat 文字列は、書式指定子の組み合わせを使用して作成します。次の表に、カスタム日時文字列を作成するためによく使用される書式指定子を示します。
書式指定子 | 説明 | 例 |
"a" | 午前/午後 | 午前 |
"d" | 月通算日 | 1 |
"dd" | 月通算日 | 01 |
"EEE" | 省略形の曜日 | 月 |
"EEEE" | 完全形の曜日 | 月曜日 |
"EEEEE" | 1 文字の曜日 | 月 |
"EEEEEE" | 短縮形の曜日 | 月 |
"h" または "K" | 12 時間制の時間 | 1 |
"hh" または "KK" | 12 時間制の時間 | 01 |
"H" または "k" | 24 時間制の時間 | 13 |
"HH" または "kk" | 24 時間制の時間 | 13 |
"m" | 分 | 1 |
"mm" | 分 | 01 |
"M" | 月番号 | 1A |
"MM" | 月番号 | 01 |
"MMM" | 月の省略名 | 9 月 |
"MMMM" | 月の完全名 | 9 月 |
"MMMMM" | 短い月名 | 9 |
"s" | 秒 | 1 |
"ss" | 秒 | 01 |
"yy" | 省略形の年 | 16 |
"yyyy" | 完全な年 | 2016 |
"z" | タイムゾーン | PST |
その他の文字 | 文字が結果の文字列にコピーされます。 | yyyy-MM a 2016-03 |
通貨、小数、パーセント、指数などの書式で数値を表示します。次のコードは、NSNumberFormatter を使用して、FlexGrid の列にローカライズした通貨書式を適用します。
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
numberFormatter.locale = NSLocale.currentLocale()
let col = flexGrid.columns.objectAtIndex(0)
col.formatter = numberFormatter
NSNumberFormatter *numberFormatter = [[NSDateFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
GridColumn *col = [flexGrid.columns objectAtIndex:0];
col.formatter = numberFormatter;
NSNumberFormatter numberFormatter = new NSNumberFormatter();
numberFormatter.NumberStyle = NSNumberFormatterStyle.Currency;
numberFormatter.Locale = NSLocale.CurrentLocale;
Column col = flexGrid.Columns.GetItem<Column>(0);
col.Formatter = numberFormatter;
フォーマッタに加えて、FlexGrid では、わずか 1 行のコードで数値列の書式設定を行う、より簡単な方法も提供されています。format プロパティを、一般的な書式のリストにある既知の文字列に設定するだけで、列の数値書式を指定できます。この文字列は 1 文字または 2 文字から成ります。最初の文字は書式指定子、2 番目の文字はオプションの精度指定子です。
たとえば、次の書式文字列は、生の値を小数点以下 2 桁の現地通貨に変換して表示します。
書式文字列では、大文字小文字は区別されません。次の表に、FlexGrid が現在サポートしている数値書式文字列を示します。
書式指定子 | 名前 | 精度指定子 | 例 |
"C" または "c" | 通貨 | 小数点以下桁数 | 123.4567 (C2) a $123.46 |
"D" または "d" | 10 進数 | 最小桁数 | 1234 (D6) a 001234 |
"E" または "e" | 指数 | 小数点以下桁数 | 1,234 (E2) a 1.23E3 |
"F" または "f" | 固定小数点 | 小数点以下桁数 | -1234.56 (F4) a -1234.5600 |
"G" または "g" | 一般 | 有効桁数 | 123.45 (G4) a 123.5 |
"N" または "n" | 数値 | 目的の小数点以下桁数 | 1234 (N1) a 1234.0 |
"P" または "p" | パーセント | 目的の小数点以下桁数 | 1 (P2) a 100.00% |
"X" または "x" | 16 進数 | 結果の文字列の目的の桁数 | 123 (X2) a 7B |
数値または日付/時刻以外の列を書式設定する必要がある場合は、「カスタムセル」および「データマッピング」トピックを参照してください。