Sunday, May 12, 2013

NSString localizedStringWithFormat example ios


localizedStringWithFormat:

Returns a string created by using a given format string as a template into which the remaining argument values are substituted according to the user's default locale.
+ (id)localizedStringWithFormat:(NSString *)format ...
Parameters
format
A format string. See “Formatting String Objects” for examples of how to use this method, and “String Format Specifiers” for a list of format specifiers. This value must not be nil.

Important: Raises an NSInvalidArgumentException if format is nil.
...
A comma-separated list of arguments to substitute into format.
Return Value[ NSString localizedStringWithFormat example ]
A string created by using format as a template into which the following argument values are substituted according to the formatting information to the user's default locale.
Discussion[ NSString localizedStringWithFormat example ]
This method is equivalent to using initWithFormat:locale: and passing [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] as the locale argument.
As an example of formatting, this method replaces the decimal according to the locale in %f and %d substitutions, and calls descriptionWithLocale: instead of description where necessary.
This code excerpt creates a string from another string and a float:
NSString *myString = [NSString localizedStringWithFormat:@"%@:  %f\n", @"Cost", 1234.56];
The resulting string has the value “Cost: 1234.560000\n” if the locale is en_US, and “Cost: 1234,560000\n” if the locale is fr_FR.
[ NSString localizedStringWithFormat example ]
NSString *myString = [NSString localizedStringWithFormat:@"%@: %f\n", @"Cost", 1234.56];
[ NSString localizedStringWithFormat example ]
[UILabel alloc] initWithFrame:CGRectMake(80.0f, 90.0f, 225.0f, 40.0f)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
int bytes = 1024 * 1024;
label.text = [NSString localizedStringWithFormat:@"0.00 MB of %.2f MB", ([self.totalFileSize floatValue] / bytes)];