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

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

概述我试图为UILabel实现这种发光效果,如下所示: 我已经将UILabel子类化,并创建了一个自定义标签类,可以添加外部阴影. 编辑:这是我在自定义Label类中用于外部阴影/发光的代码: - (void)drawTextInRect:(CGRect)rect {UIColor *insideColor;UIColor *blurColor;CGContextRef ctx = UIGrap 我试图为UILabel实现这种发光效果,如下所示:

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

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

- (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];}

但这给了我以下结果

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

谢谢!

解决方法 我将史蒂芬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(),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,0);      [[UIcolor blackcolor] setFill];    CGContextFillRect(UIGraphicsGetCurrentContext(),CGRectMake(0,size.wIDth,size.height));    UIImage *blackSquare = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();    return blackSquare;}- (CGImageRef)createMaskWithSize:(CGSize)size shape:(voID (^)(voID))block {    UIGraphicsBeginImageContextWithOptions(size,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;}
总结

以上是内存溢出为你收集整理的objective-c – 使用内部阴影在UILabel中发光效果全部内容,希望文章能够帮你解决objective-c – 使用内部阴影在UILabel中发光效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存