4

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 ,,,

https://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?

Luke
  • 11,426
  • 43
  • 60
  • 69
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
  • Why are you concerned about subclassing? – Ben Scheirman Jul 06 '11 at 13:41
  • you don't want to inherit UILabel and make a new class because 'it will make heavy when you update every second'. I think you might want to revisit inheritance… – jakev Jul 06 '11 at 13:45
  • I saw an example making subclas of UILabel, and adding properties, the output glow was fantastic, but It become late when you update time(seconds) every second, so I don't want to use class's object. – Chatar Veer Suthar Jul 06 '11 at 13:49
  • @NS6String, What means "I think you might want to revisit inheritance"? – Chatar Veer Suthar Jul 07 '11 at 05:00
  • What they mean is that subclassing (when done right) adds virtually no overhead. And "then making Label from that class" is a cost-free down-cast operation -- it's not creating a new UILabel. You should read up a little more on inheritance and subclassing and how it all works. – Hot Licks Jul 07 '11 at 16:51

2 Answers2

2

Take a look at this: Pragmatic Forums

or maybe this: Stackoverflow

Community
  • 1
  • 1
tazboy
  • 1,685
  • 5
  • 23
  • 39
0

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

Beleg
  • 362
  • 2
  • 23