一段关于文字音乐高光效果的设置代码摘录.

本段代码转自一个示例代码.


播放器文字高光效果:


-(void)settext:(NSString *)te
{
    if (text!=nil)
    {
        [text release];
        text = nil;
    }
    text = [te retain];
    
    CGSize size1 = [text sizeWithFont:FONT];
    NSLog(@"%f %f",size1.width,size1.height);
    CGSize size = [text sizeWithFont:FONT constrainedToSize:self.frame.size
                       lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%f %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];
    [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
    
    [self setNeedsDisplay];
}
-(void)timerFired:(NSTimer*)timer
{
    NSLog(@"timer运行");
    //[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(@"行数:%d 烈数:%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, 1);
        CGContextFillRect(context, CGRectMake(0.0,0.0,linew,line*linehighe));
    }
    if (linex>0)
    {
        CGContextSetRGBFillColor(context, 1, 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, 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++;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值