What is best way to make UILabel Glow and look bright?

好久不见. 提交于 2019-12-07 10:39:06

问题


I made UILabel showing current time, I want that time(UILabel) glow on the screen, I tried many answers which I found via google, but no one is properply working, need like this ,,,

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

I tried some think like as below,

In 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];

for making background black, then I did as

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];

after ViewDidLoad

In the body of method drawTextInRect, I did as follow

 - (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);
  }

What should I do ? I don't want to inherit UILabel and make new class, because it will make heavy when u update every second, then it will update after 2 or 3 seconds, even your timer is being called twice in one second,

any suggestion?


回答1:


Take a look at this: Pragmatic Forums

or maybe this: Stackoverflow




回答2:


Take a look at this code on GitHub, It looks great, however so far I have been make it work with io7. Whats I figure that out I will post an update here.

https://github.com/andrewgleave/TextGlowDemo



来源:https://stackoverflow.com/questions/6596718/what-is-best-way-to-make-uilabel-glow-and-look-bright

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!