iPhone iOS how to draw text on a CALayer using drawLayer:inContext:? -
i have simple scrolling graph uses method draw itself. i'm looking way add text label graph, cannot find how this.
i tried:
[@"test" drawinrect: cgrectmake(0, 0, 10, 10) withfont:[uifont systemfontofsize:8]]; and error messages saying invalid context 0x0. how modify code below point text drawing code correct context?
-(void)drawlayer:(calayer*)l incontext:(cgcontextref)context { // fill in background cgcontextsetfillcolorwithcolor(context, graphbackgroundcolor()); cgcontextfillrect(context, layer.bounds); // draw grid lines // drawgridlines(context, 0.0, 32.0); // draw graph cgpoint lines[64]; int i; // x for(i = 0; < 32; ++i) { lines[i*2].x = i; lines[i*2].y = -(xhistory[i] * 480.0)-10; lines[i*2+1].x = + 1; lines[i*2+1].y = -(xhistory[i+1] * 480.0)-10; } cgcontextsetstrokecolorwithcolor(context, graphzcolor()); cgcontextstrokelinesegments(context, lines, 64); }
for each thread, uikit maintains stack of graphics contexts. can context @ top of current thread's stack calling uigraphicsgetcurrentcontext.
when use drawinrect:withfont:, string draws context returned uigraphicsgetcurrentcontext, need make uigraphicsgetcurrentcontext return context argument of drawlayer:incontext: using uigraphicspushcontext. when done drawing, must call uigraphicspopcontext. thus:
-(void)drawlayer:(calayer*)l incontext:(cgcontextref)context { uigraphicspushcontext(context); { // fill in background cgcontextsetfillcolorwithcolor(context, graphbackgroundcolor()); cgcontextfillrect(context, layer.bounds); // etc. } uigraphicspopcontext(); }
Comments
Post a Comment