Michael Bordelon
Michael Bordelon

Reputation: 259

Unicode Character in Core Graphics

I have an iPad app where I am storing some unicode (non-ASCII) data in a SQLite DB. Later, I retrieve that data and need to write it to a pdf. All ASCII data does fine, but the unicode data is presenting as "encoded characters"

The process goes like this: Retrieve the data and write it to the console. This goes fine. Here is the output to the console: LOOKUP:↑ to enable all activities:FOR:COORDINATIONGOAL:

(The little up arrow is my unicode character)

At this point, the data is stored in an NSString. So next, I convert it to a char so I can use it in core graphics:

NSString *t = [data objectForKey:pi.persistencekey];
char *text = " ";

if ([t length] > 0) {           
  text = [t UTF8String];            
}

CGContextShowTextAtPoint (pdfContext,  pi.x, pageRect.size.height - pi.y, text, strlen(text));

The PDF generates fine, but the text generated shows strange characters where the up arrow is supposed to be.

I have tried other decoding methods and none work.

Thanks in advance for any help.

Upvotes: 2

Views: 503

Answers (1)

amattn
amattn

Reputation: 10065

CGContextShowTextAtPoint has some known weaknesses with drawing unicode. Basically it doesn't.

You have two options:

  1. NSString's drawAtPoint and it's friends. They can be found by searching doc for UIStringDrawing.

  2. or if you need to go low-level, Core Text.

Most of the time (99%) 1. is good enough.

Upvotes: 1

Related Questions