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?
Take a look at this: Pragmatic Forums
or maybe this: Stackoverflow
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://stackoverflow.com/questions/6596718/what-is-best-way-to-make-uilabel-glow-and-look-bright