An excerpt from the setting code for the text music highlight effect.

Source: Internet
Author: User

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 ++;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.