Какой лучший способ сделать UILabel Glow и выглядеть ярко? - PullRequest
4 голосов
/ 06 июля 2011

Я заставил UILabel показывать текущее время, я хочу, чтобы это время (UILabel) светилось на экране, я пробовал много ответов, которые нашел через google, но никто не работает должным образом, нужно как то ,,,

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 секунды, даже если ваш таймер вызывается дважды за одну секунду,

есть предложения?

Ответы [ 2 ]

2 голосов
/ 07 июля 2011

Взгляните на это: Прагматичные форумы

или, может быть, это: Stackoverflow

0 голосов
/ 22 февраля 2014

Посмотрите на этот код на GitHub, он выглядит великолепно, однако до сих пор я работал с io7. Что я понял, что я опубликую обновление здесь.

https://github.com/andrewgleave/TextGlowDemo

...