什么是制作UILabel Glow并且看起来很亮的最佳方法?

时间:2011-07-06 12:56:04

标签: iphone uilabel glow

我让UILabel显示当前时间,我希望那个时间(UILabel)在屏幕上发光,我尝试了很多答案,我通过谷歌发现,但没有人正常工作, 需要这样,,,

http://i.stack.imgur.com/4REJp.png

我试过一些想法如下,

在ViewDidLoad

 colorOne = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
colorTwo = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];

//Create the gradient and add it to our view's root layer
gradientLayer = [[[CAGradientLayer alloc] init] autorelease];
gradientLayer.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
[gradientLayer setColors:[NSArray arrayWithObjects:(id)colorOne.CGColor,      (id)colorTwo.CGColor, nil]];
[self.view.layer insertSublayer:gradientLayer atIndex:0];

使背景变黑,然后我做了

CGRect rect = CGRectMake(15, 130, 320, 200);
    label = [[UILabel alloc] initWithFrame:rect];
    label.backgroundColor = [UIColor clearColor];
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    currentDateTime = [NSDate date];
    [dateFormatter setDateFormat:@"hh:mm:ss "];

    label.font=[UIFont fontWithName:@"DBLCDTempBlack" size:60.0];
    label.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

    glowOffset = CGSizeMake(10.0, 2.0);
    glowColor = label.textColor;
    glowAmount = 150.0f;
    //[self drawRect:rect];
    [self drawTextInRect:rect];

    [self.view addSubview:label];

在ViewDidLoad

之后

在方法drawTextInRect的主体中,我做了如下

 - (void)drawTextInRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetShadow(context, glowOffset, glowAmount);
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

   CGColorRef color = CGColorCreate(colorSpace,    CGColorGetComponents(glowColor.CGColor));
CGContextSetShadowWithColor(context, glowOffset, glowAmount, color);

  // [label drawTextInRect:rect]; 

   /*
   I can't understand this because I tried an code which was doing glow but was making     class that inheriting UILabel, then making Label from that class
   */

   CGColorRelease(color);
   CGColorSpaceRelease(colorSpace);
   CGContextRestoreGState(context);
  }

我该怎么办? 我不想继承UILabel并创建新类,因为当你每秒更新时它会变重,然后它会在2或3秒后更新,甚至你的计时器在一秒内被调用两次,

任何建议?

2 个答案:

答案 0 :(得分:2)

看看这个: Pragmatic Forums

或者这个: Stackoverflow

答案 1 :(得分:0)

看一下GitHub上的这段代码,它看起来很棒,但到目前为止,我一直在使用io7。我想是什么,我会在这里发布更新。

https://github.com/andrewgleave/TextGlowDemo