is it possible to create such a UILabel with inner and outer shadow?

i only know shadowColor and shadowOffset

zoomed:

thanks!

解决方案

The answer by dmaclach is only suitable for shapes that can easily be inverted. My solution is a custom view that works with any shape and also text. It requires iOS 4 and is resolution independent.

First, a graphical explanation of what the code does. The shape here is a circle.

The code draws text with a white dropshadow. If it's not required, the code could be refactored further, because the dropshadow needs to be masked differently. If you need it on an older version of iOS, you would have to replace the block and use an (annoying) CGBitmapContext.

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

}

- (void)drawRect:(CGRect)rect {

UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:40.0f];

CGSize fontSize = [text_ sizeWithFont:font];

CGImageRef mask = [self createMaskWithSize:rect.size shape:^{

[[UIColor blackColor] setFill];

CGContextFillRect(UIGraphicsGetCurrentContext(), rect);

[[UIColor whiteColor] setFill];

// custom shape goes here

[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];

[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];

}];

CGImageRef cutoutRef = CGImageCreateWithMask([self blackSquareOfSize:rect.size].CGImage, mask);

CGImageRelease(mask);

UIImage *cutout = [UIImage imageWithCGImage:cutoutRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];

CGImageRelease(cutoutRef);

CGImageRef shadedMask = [self createMaskWithSize:rect.size shape:^{

[[UIColor whiteColor] setFill];

CGContextFillRect(UIGraphicsGetCurrentContext(), rect);

CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 1), 1.0f, [[UIColor colorWithWhite:0.0 alpha:0.5] CGColor]);

[cutout drawAtPoint:CGPointZero];

}];

// create negative image

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

[[UIColor blackColor] setFill];

// custom shape goes here

[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];

UIImage *negative = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRef innerShadowRef = CGImageCreateWithMask(negative.CGImage, shadedMask);

CGImageRelease(shadedMask);

UIImage *innerShadow = [UIImage imageWithCGImage:innerShadowRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];

CGImageRelease(innerShadowRef);

// draw actual image

[[UIColor whiteColor] setFill];

[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -0.5) withFont:font];

[[UIColor colorWithWhite:0.76 alpha:1.0] setFill];

[text_ drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];

// finally apply shadow

[innerShadow drawAtPoint:CGPointZero];

}

ioslabel阴影_UILabel的内阴影相关推荐

  1. ioslabel阴影,UILabel的内阴影

    is it possible to create such a UILabel with inner and outer shadow? i only know shadowColor and sha ...

  2. css3 box-shadow阴影(内外阴影与发光)讲解

    基础说明:     外阴影:box-shadow: X轴  Y轴  Rpx  color;     属性说明(顺序依次对应): 阴影的X轴(可以使用负值)    阴影的Y轴(可以使用负值)    阴影 ...

  3. CSS3与页面布局学习笔记(六)——CSS3新特性(阴影、动画、渐变、变形( transform)、透明、伪元素等)...

    一.阴影 1.1.文字阴影 text-shadow <length>①: 第1个长度值用来设置对象的阴影水平偏移值.可以为负值 <length>②: 第2个长度值用来设置对象的 ...

  4. (24)css3盒子阴影

    1.边框阴影 在 CSS3 中,box-shadow 属性用于对盒子边框添加阴影. 2.边框阴影语法 box-shadow 属性向盒子添加阴影,属性值有2-4 个长度值.可选的颜色值以及可选 的 in ...

  5. css3 box-shadow阴影(外阴影与外发光)讲解

    基础说明:     外阴影:box-shadow: X轴  Y轴  Rpx  color;     属性说明(顺序依次对应): 阴影的X轴(可以使用负值)    阴影的Y轴(可以使用负值)    阴影 ...

  6. CSS3新特性(阴影、动画、渐变、变形、伪元素等) CSS3与页面布局学习总结——CSS3新特性(阴影、动画、渐变、变形、伪元素等)...

    目录 一.阴影 1.1.文字阴影 1.2.盒子阴影 二.背景 2.1.背景图像尺寸 2.2.背景图像显示的原点 三.伪元素 3.1.before 3.2.after 3.3.清除浮动 四.圆角与边框 ...

  7. CSS3阴影实现方法及技巧全解

    CSS3阴影实现方法及技巧全解 网页设计中常常要用到阴影的效果,通过阴影可以比较容易突出一个元素,在没有CSS3的时候,一般都用图片做阴影效果,而现在通过使用CSS3的text-shadow和box- ...

  8. CSS3 属性选择器 伪类选择器 盒模型 圆角 阴影 CSS定位和浮动

    HTML是网页的裸框架,但是现在已经是2020年了,你再做出一个80年代的网页来,恐怕是没HR要你了.所以学习CSS的重要性可想而知,CSS用途很广,是一门独立的编程语言,能美化网页,也能进一步提高自 ...

  9. box-shadow盒子阴影几种用法

    box-shadow盒子阴影 语法:     外阴影:box-shadow: X轴  Y轴  Rpx   color;     属性说明(顺序依次对应): 阴影的X轴(可以使用负值)    阴影的Y轴 ...

最新文章

  1. html引入php文件中的函数,在b2core框架和simple_html_dom.php文件里面都有一个load()函数...
  2. 原始代理需要改进的地方
  3. 获取执行计划的N种方式
  4. 借款人死亡后,贷款必须由家属偿还吗?
  5. 腐蚀和膨胀(erode and dilate)
  6. windows安装npm教程
  7. 基于Linux下 Oracle 备份策略(RMAN)
  8. 搜狐校园“情感分析×推荐排序“算法大赛 AutoX方案 转载poteman
  9. 【生信进阶练习1000days】day4-Annotation包中mapIds函数的使用
  10. [GIS教程] 6 空间数据采集与处理-GIS数据源分类及其特征、采集与处理的基本流程、4D产品
  11. 将k8容器中文件下载到本地
  12. WordArt怎样把endnote的关键词生成词云
  13. Casbin之基于角色的控制访问RBAC
  14. xtu ACM Eason
  15. 房产估值模型训练及预测结果
  16. 给诸位想换工作的IT同学的一些建议
  17. [从头读历史] 第257节 孔子和他的《春秋》 [BC537至BC479]
  18. MLE, MAP and LSM
  19. input元素及属性
  20. 20个经典管理学定律

热门文章

  1. centos7不识别固态硬盘_固态硬盘分区与不分区哪个好【详细介绍】
  2. SSD浅层网络_目标检测SSD
  3. python输出出现频率最高的字母_用Python实现搜索某一网页中出现频率最高的前N个英文单词 输入: 网址,N值 输出:按出现频率由高到低排...
  4. 基于bs4库的HTML内容查找方法
  5. 中文命名代码示例之Spring Boot 1.3.3演示
  6. 31 socket客户端. 服务器 异常 语法
  7. C# 算法题系列(一) 两数之和、无重复字符的最长子串
  8. 前端 javascript 数据类型 字典
  9. python安装mysqlclient模块报fatal error: Python.h:解决方法
  10. Python学习笔记(四十六)网络编程(2)— UDP编程