使用内部阴影的 UILabel 中的发光效果
Posted
技术标签:
【中文标题】使用内部阴影的 UILabel 中的发光效果【英文标题】:Glowing effect in a UILabel using inner shadow 【发布时间】:2012-07-24 10:34:51 【问题描述】:我正在尝试为 UILabel 实现这种发光效果,如下所示:
我对 UILabel
进行了子类化,并创建了一个添加外部阴影的自定义标签类。
编辑:这是我在自定义标签类中用于外部阴影/发光的代码:
- (void)drawTextInRect:(CGRect)rect
UIColor *insideColor;
UIColor *blurColor;
CGContextRef ctx = UIGraphicsGetCurrentContext();
insideColor =[UIColor colorWithRed:255/255.0 green:255/255.0 blue:191/255.0 alpha:1];
blurColor =[UIColor orangeColor];
CGContextSetFillColorWithColor(ctx, insideColor.CGColor);
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), self.glowAmount, blurColor.CGColor);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
[self.text drawInRect:self.bounds withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
但这给了我以下结果
正如您所见,由于缺少内阴影,这缺乏所需的效果。任何人都可以建议如何实现这一点吗?
谢谢!
【问题讨论】:
ios - How to achieve emboss effect for the text on UILabel? Rob Mayoff 的答案可能是重复的 - 应该可以为您提供所需的内部阴影。 这使用了一个 UIImage。我们不能直接在 - (void)drawTextInRect:(CGRect)rect 方法中对 UILabel 文本执行此操作吗? 你能贴出所有与标签相关的代码吗? @Szwedo :我已经编辑了我的问题并发布了当前使用的代码。 Nayan,你能发布你更新后的适用于 ios7 的代码吗? 【参考方案1】:我将 Steven XM 的答案推荐给 Inner Shadow in UILabel 。这是一个很大的帮助。
这是我为实现结果所做的工作,但我想知道这是否可以更优化?
-(void)setInnerGlowWithColor:(UIColor *)shadowColor fillColor:(UIColor *)insideColor inRect:(CGRect)rect
UIFont *font = self.font;
// UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17];
CGSize fontSize = [self.text sizeWithFont:font];
/**** Following are the steps to create an inside shadow ****/
// STEP 1 : Create a image mask of your text.
CGImageRef mask = [self createMaskWithSize:rect.size shape:^
[[UIColor blackColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
[[UIColor whiteColor] setFill];
// custom shape goes here
[self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];
];
// STEP 2 : Invert that mask.
CGImageRef cutoutRef = CGImageCreateWithMask([self blackSquareOfSize:rect.size].CGImage, mask);
CGImageRelease(mask);
UIImage *cutout = [UIImage imageWithCGImage:cutoutRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGImageRelease(cutoutRef);
// STEP 3 : Use this inverted mask to draw a shadow around the inside edges of the text.
CGImageRef shadedMask = [self createMaskWithSize:rect.size shape:^
[[UIColor whiteColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
//****************For inner shadow/glow
NSLog(@"in custom label----> %f",self.glowAmount);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), self.glowAmount, shadowColor.CGColor);
[cutout drawAtPoint:CGPointZero];
];
// STEP 4 : Create negative image
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[shadowColor setFill];
// custom shape goes here
[self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];
UIImage *negative = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// STEP 5 : Create the shadow image
CGImageRef innerShadowRef = CGImageCreateWithMask(negative.CGImage, shadedMask);
CGImageRelease(shadedMask);
UIImage *innerShadow = [UIImage imageWithCGImage:innerShadowRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGImageRelease(innerShadowRef);
// STEP 6 : Draw actual text
[insideColor setFill];
[self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];
// STEP 7 : Finally apply the shadow image
[innerShadow drawAtPoint:CGPointZero];
- (UIImage*)blackSquareOfSize:(CGSize)size
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[[UIColor blackColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height));
UIImage *blackSquare = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blackSquare;
- (CGImageRef)createMaskWithSize:(CGSize)size shape:(void (^)(void))block
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
block();
CGImageRef shape = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(shape),
CGImageGetHeight(shape),
CGImageGetBitsPerComponent(shape),
CGImageGetBitsPerPixel(shape),
CGImageGetBytesPerRow(shape),
CGImageGetDataProvider(shape), NULL, false);
return mask;
【讨论】:
以上是关于使用内部阴影的 UILabel 中的发光效果的主要内容,如果未能解决你的问题,请参考以下文章