我让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秒后更新,甚至你的计时器在一秒内被调用两次,
有什么建议吗?
最佳答案 看看这个:
或者这个:
Stackoverflow