objective-c – 使用内部阴影在UILabel中发光效果

前端之家收集整理的这篇文章主要介绍了objective-c – 使用内部阴影在UILabel中发光效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图为UILabel实现这种发光效果,如下所示:

我已经将UILabel子类化,并创建了一个自定义标签类,可以添加外部阴影.

编辑:这是我在自定义Label类中用于外部阴影/发光的代码

  1. - (void)drawTextInRect:(CGRect)rect {
  2. UIColor *insideColor;
  3. UIColor *blurColor;
  4. CGContextRef ctx = UIGraphicsGetCurrentContext();
  5.  
  6. insideColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:191/255.0 alpha:1];
  7. blurColor =[UIColor orangeColor];
  8.  
  9.  
  10. CGContextSetFillColorWithColor(ctx,insideColor.CGColor);
  11. CGContextSetShadowWithColor(ctx,CGSizeMake(0,0),self.glowAmount,blurColor.CGColor);
  12. CGContextSetTextDrawingMode(ctx,kCGTextFillStroke);
  13.  
  14. [self.text drawInRect:self.bounds withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
  15. }

但这给了我以下结果

正如你所看到的,由于缺少内阴影,这缺乏预期的效果.谁能建议如何实现这一目标?

谢谢!

解决方法

我将史蒂芬XM的答案提到了 Inner Shadow in UILabel.这是一个很大的帮助.

这是我为实现结果所做的工作,但我想知道这是否可以更优化?

  1. -(void)setInnerGlowWithColor:(UIColor *)shadowColor fillColor:(UIColor *)insideColor inRect:(CGRect)rect
  2. {
  3.  
  4. UIFont *font = self.font;
  5. // UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17];
  6. CGSize fontSize = [self.text sizeWithFont:font];
  7.  
  8.  
  9. /**** Following are the steps to create an inside shadow ****/
  10.  
  11. // STEP 1 : Create a image mask of your text.
  12.  
  13. CGImageRef mask = [self createMaskWithSize:rect.size shape:^{
  14. [[UIColor blackColor] setFill];
  15. CGContextFillRect(UIGraphicsGetCurrentContext(),rect);
  16. [[UIColor whiteColor] setFill];
  17. // custom shape goes here
  18. [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2),0) withFont:font];
  19. }];
  20.  
  21.  
  22. // STEP 2 : Invert that mask.
  23.  
  24. CGImageRef cutoutRef = CGImageCreateWithMask([self blackSquareOfSize:rect.size].CGImage,mask);
  25. CGImageRelease(mask);
  26.  
  27. UIImage *cutout = [UIImage imageWithCGImage:cutoutRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
  28. CGImageRelease(cutoutRef);
  29.  
  30.  
  31.  
  32. // STEP 3 : Use this inverted mask to draw a shadow around the inside edges of the text.
  33.  
  34. CGImageRef shadedMask = [self createMaskWithSize:rect.size shape:^{
  35. [[UIColor whiteColor] setFill];
  36. CGContextFillRect(UIGraphicsGetCurrentContext(),rect);
  37. //****************For inner shadow/glow
  38. NSLog(@"in custom label----> %f",self.glowAmount);
  39. CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(),shadowColor.CGColor);
  40. [cutout drawAtPoint:CGPointZero];
  41. }];
  42.  
  43.  
  44. // STEP 4 : Create negative image
  45.  
  46. UIGraphicsBeginImageContextWithOptions(rect.size,NO,0);
  47. [shadowColor setFill];
  48. // custom shape goes here
  49. [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2),0) withFont:font];
  50. UIImage *negative = UIGraphicsGetImageFromCurrentImageContext();
  51. UIGraphicsEndImageContext();
  52.  
  53.  
  54. // STEP 5 : Create the shadow image
  55.  
  56. CGImageRef innerShadowRef = CGImageCreateWithMask(negative.CGImage,shadedMask);
  57. CGImageRelease(shadedMask);
  58. UIImage *innerShadow = [UIImage imageWithCGImage:innerShadowRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
  59. CGImageRelease(innerShadowRef);
  60.  
  61.  
  62. // STEP 6 : Draw actual text
  63.  
  64. [insideColor setFill];
  65. [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2),0) withFont:font];
  66.  
  67.  
  68. // STEP 7 : Finally apply the shadow image
  69.  
  70. [innerShadow drawAtPoint:CGPointZero];
  71.  
  72. }
  73.  
  74.  
  75. - (UIImage*)blackSquareOfSize:(CGSize)size {
  76. UIGraphicsBeginImageContextWithOptions(size,0);
  77. [[UIColor blackColor] setFill];
  78. CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake(0,size.width,size.height));
  79. UIImage *blackSquare = UIGraphicsGetImageFromCurrentImageContext();
  80. UIGraphicsEndImageContext();
  81. return blackSquare;
  82. }
  83.  
  84.  
  85. - (CGImageRef)createMaskWithSize:(CGSize)size shape:(void (^)(void))block {
  86. UIGraphicsBeginImageContextWithOptions(size,0);
  87. block();
  88. CGImageRef shape = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
  89. UIGraphicsEndImageContext();
  90. CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(shape),CGImageGetHeight(shape),CGImageGetBitsPerComponent(shape),CGImageGetBitsPerPixel(shape),CGImageGetBytesPerRow(shape),CGImageGetDataProvider(shape),NULL,false);
  91. return mask;
  92. }

猜你在找的C&C++相关文章