让 UILabel 发光并看起来明亮的最佳方法是什么?

发布于 2024-11-18 11:56:15 字数 2504 浏览 4 评论 0原文

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

https://i.sstatic.net/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 秒后更新,甚至你的计时器在一秒钟内被调用两次,

有什么建议吗?

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.sstatic.net/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-11-25 11:56:15

看看这个:
实用论坛

或者可能是这样:
Stackoverflow

Take a look at this:
Pragmatic Forums

or maybe this:
Stackoverflow

浅笑依然 2024-11-25 11:56:15

在 GitHub 上看一下这段代码,它看起来很棒,但到目前为止我一直让它与 io7 一起工作。我知道了什么,我会在这里发布更新。

https://github.com/andrewgleave/TextGlowDemo

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文