tags:

views:

77

answers:

2

How to draw a currency symbol in a custom label using CGContextShowTextAtPoint method in draw rect. Here the symbol is in string format.

Any help!! Thanks

A: 

I don't really get what you're asking, checking the documentation, the method would look something like that:

 CGContextRef ctx = UIGraphicsGetCurrentContext(); 
 const char *string = "$";
 CGContextShowTextAtPoint (ctx, 160, 240, string, 1);

Haven't tested it, but this should draw $ in the center of the screen.
BTW, why not use images?

~ Natanavra.

natanavra
A: 

You have to resort to C style strings, since this is what CGContextShowTextAtPoint() requires. In order to correctly handle the locale (the currency symbol changes with the locale) you must use setlocale(), then you format your string using strfmon() and finally you pass the string created with strfmon() to CGContextShowTextAtPoint().

Documentation is available as follows from the terminal:

man 3 setlocale
man 3 strfmon

EDIT/UPDATE: For your information, strfmon() internally uses struct lconv. The structure can be retrieved with the function localeconv(). See man 3 localeconv for a detailed description of the fields available in the structure.

for instance, try the following simple C program setting different locales

#include <stdio.h>
#include <locale.h>
#include <monetary.h>

int main(void)
{
    char buf[BUFSIZ];
    double val = 1234.567;

    /* use your current locale */
    setlocale(LC_ALL, ""); 

    /* uncomment the next line and try this to use italian locale */
    /* setlocale(LC_ALL, "it_IT"); */ 
    strfmon(buf, sizeof buf, "You owe me %n (%i)\n", val, val);

    fputs(buf, stdout);
    return 0;
}

The following uses localeconv():

#include <stdio.h>
#include <limits.h>
#include <locale.h>

int main(void)
{
    struct lconv l;
    int i;

    setlocale(LC_ALL, "");
    l = *localeconv();

    printf("decimal_point = [%s]\n", l.decimal_point);
    printf("thousands_sep = [%s]\n", l.thousands_sep);

    for (i = 0; l.grouping[i] != 0 && l.grouping[i] != CHAR_MAX; i++)
        printf("grouping[%d] = [%d]\n", i, l.grouping[i]);

    printf("int_curr_symbol = [%s]\n", l.int_curr_symbol);
    printf("currency_symbol = [%s]\n", l.currency_symbol);
    printf("mon_decimal_point = [%s]\n", l.mon_decimal_point);
    printf("mon_thousands_sep = [%s]\n", l.mon_thousands_sep);
    printf("positive_sign = [%s]\n", l.positive_sign);
    printf("negative_sign = [%s]\n", l.negative_sign);
}
unforgiven
a C style string can also be defined like I did in my answer.a char pointer is a C string.
natanavra
Sure. The problem here is not how to define a C string, rather how to correctly print locale dependent symbols (currency, time etc) inside it. The proposed solution uses the ISO C99 standard functions to deal with currency symbols.
unforgiven
From above solution i only get currency code of different lacale. I need to get the symbol representing currency of a country.How can i retrieve symbol and put it in showTextAtPoint.
diana
The examples show how to retrieve both the currency code and symbol. Using the struct lconv the symbol is the field named currency_symbol and the code is the field int_curr_symbol. With strfmon() using %n the double argument is formatted as a national monetary amount printed using the currency symbol, and using %i the double argument is formatted as an international monetary amount using the currency code. This is what the ISO C99 standard provides.
unforgiven
decimal_point = [,]thousands_sep = [. ]int_curr_symbol = [EUR ]currency_symbol = [Eu]mon_decimal_point = [,]mon_thousands_sep = [.]positive_sign = []negative_sign = [-]This is the output i got for locale it_IT.
diana
I solved the problem in an alternate way. Instead of showTextAtPoint i use drawAtPoint. That solved my problem. Here i get the currency symbol from currencySymbol method of NSNuberformatter class.
diana