This code is converted from a sample code.
Player text highlight:
-(Void) settext :( nsstring *) Te
{
If (text! = Nil)
{
[Text release];
TEXT = nil;
}
TEXT = [Te retain];
Cgsize size1 = [text sizewithfont: font];
Nslog (@ "% F", size1.width, size1.height );
Cgsize size = [text sizewithfont: font constrainedtosize: Self. Frame. Size
Linebreakmode: uilinebreakmodewordwrap];
Nslog (@ "% F", size. Width, size. Height );
Linenum = size. Height/size1.height;
Hight = size. height;
Len = 0;
If ([t isvalid])
{
[T invalidate];
[T release];
}
T = [[nstimer alloc] initwithfiredate: [nsdate date] interval: 0.01 target: Self selector: @ selector (timerfired :) userinfo: Nil repeats: Yes];
[[Nsunloop currentrunloop] addtimer: T formode: nsdefaultrunloopmode];
[Self setneedsdisplay];
}
-(Void) timerfired :( nstimer *) Timer
{
Nslog (@ "timer Run ");
// [T invalidate];
// [T release];
[Self setneedsdisplay];
}
-(Void) drawrect :( cgrect) rect
{
Int linew = rect. Size. width;
Int linehighe = hight/linenum;
Int line = Len/linew;
Int LinEx = Len % linew;
Nslog (@ "number of lines: % d strong: % d", line, LinEx );
Cgcontextref context = uigraphicsgetcurrentcontext ();
Cgcontextsavegstate (context );
Cgcontextsavegstate (context );
Cgcontexttranslatectm (context, 0, rect. Size. Height );
Cgcontextscalectm (context, 1.0,-1.0 );
If (line> 0)
{
Cgcontextsetrgbfillcolor (context, 1, 1, 1 );
Cgcontextfillrect (context, cgrectmake (0.0, 0.0, linew, line * linehighe ));
}
If (LinEx> 0)
{
Cgcontextsetrgbfillcolor (context, 1, 1, 1 );
Cgcontextfillrect (context, cgrectmake (0.0, line * linehighe, LinEx, linehighe ));
}
Cgcontextsetrgbfillcolor (context, 0.5, 0.1, 0.8, 1 );
Cgcontextfillrect (context, cgrectmake (LinEx, line * linehighe, linew-LinEx, linehighe ));
Cgcontextfillrect (context, cgrectmake (0.0, (LINE + 1) * linehighe, linew, (linenum-1-line) * linehighe ));
Cgimageref alphamask = cgbitmapcontextcreateimage (context );
Cgcontextrestoregstate (context );
/*
Cgcontextref context = uigraphicsgetcurrentcontext ();
Cgcontextsavegstate (context );
Cgcontextsetrgbfillcolor (context, 0.5, 0.1, 0.8, 1 );
Cgcontextfillrect (context, cgrectmake (0.0, 0.0, rect. Size. width/2, rect. Size. Height ));
Cgcontextsetrgbfillcolor (context, 1, 1, 1 );
Cgcontextfillrect (context, cgrectmake (rect. Size. width/2, 0, rect. Size. width/2, rect. Size. Height ));
Cgimageref alphamask = cgbitmapcontextcreateimage (context );
Cgcontextrestoregstate (context );
*/
Cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 1 );
Cgcontextfillrect (context, rect );
Cgcontextcliptomask (context, rect, alphamask );
[[Uicolor greencolor] setfill];
[Text drawinrect: rect withfont: font];
Cgimagerelease (alphamask );
Cgcontextrestoregstate (context );
Len ++;
}