ioslabel陰影,UILabel的內陰影

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.

61533bbcf136e59a05b96b6fab26d898.png

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

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393848.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393848.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393848.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Webpack初學者介紹

Webpack is a tool that lets you compile JavaScript modules. It’s also known as a module bundler.Webpack是使您可以編譯JavaScript模塊的工具。 也稱為模塊捆綁器 。 Given a large number of files, it generates a single file (or a few files) that run your app.給…

Android Coding利器之掌握小技巧,助你Coding更上一層樓~

本文講的是Android Coding利器之掌握小技巧,助你Coding更上一層樓~,話說前幾天在網上瀏覽到一大牛寫的關于Android布局優化的文章,看后感觸很深,回過頭看看自己寫過的代碼,發現還是有不少需要改進,今天找不…

linux系統報警怎么辦,常見Linux系統故障和解決方法

常見Linux系統故障和解決方法發布時間:2020-06-06 14:48:19來源:億速云閱讀:212作者:Leah欄目:云計算這篇文章給大家分享的是常見的Linux系統故障和解決方法。在使用系統的過程中總會有各種各樣的故障,所以…

Vuex 模塊化與項目實例 (2.0)

Vuex 強調使用單一狀態樹,即在一個項目里只有一個 store,這個 store 集中管理了項目中所有的數據以及對數據的操作行為。但是這樣帶來的問題是 store 可能會非常臃腫龐大不易維護,所以就需要對狀態樹進行模塊化的拆分。 首先貼出一個邏輯比較…

click js自動點擊 vue_vue.js2.0點擊獲取自己的屬性和jquery方法

如下所示::data-index"index":dt"index"v-on:click"onclick($event,index)":data-d "JSON.stringify( item)"href"http://www.baidu.com" rel"external nofollow" rel"external nofollow"da…

Python:知識目錄

Python目錄 第一篇:數據類型部分文件操作 基礎數據類型---str 基礎數據類型---List 基礎數據類型---dict 基礎數據類型---set 基礎數據類型---bytes 數據類型的總結 文件操作------讀,寫 文件操作------使用方法 第二章:函數模塊 初識函數…

初學者css常見問題_5分鐘內學習CSS-初學者教程

初學者css常見問題關于網絡設計語言的快速教程。 (A quick tutorial on the design language of the web.) CSS (Cascading Style Sheets) is what makes web pages look good and presentable. It’s an essential part of modern web development and a must-have skill for …

leetcode39. 組合總和(回溯)

給定一個無重復元素的數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 說明: 所有數字(包括 target)都是正整數。 解集不能包含重復的…

一臉懵逼學習基于CentOs的Hadoop集群安裝與配置(三臺機器跑集群)

1:Hadoop分布式計算平臺是由Apache軟件基金會開發的一個開源分布式計算平臺。以Hadoop分布式文件系統(HDFS)和MapReduce(Google MapReduce的開源實現)為核心的Hadoop為用戶提供了系統底層細節透明的分布式基礎架構。 注…

linux批量去掉文件名前綴,linux 批量刪除某個前綴文件

1. 命令 (參考:https://blog.csdn.net/kl28978113/article/details/80271831)find ./ -name updatesites*-* -exec rm {} \;2. 舉例[rootadmin batch-create-sites]# ls2020-02-13-10-10.out logs-2020-04-07-08-00.out updatesites-2020-02-12-01-49-25.xlsx updat…

Docker - 避免啟動container后運行shell腳本執行完成后docker退出container

問題 最近在使用 Dockerfile 啟動容器,發現使用Dockerfile調用容器里面的shell,當shell執行完成以后,docker會退出容器。 分析 Docker 在執行shell的時候,是在后臺執行的;因此,在shell執行完成以后&#xf…

css畫橫線箭頭_用CSS繪制三角形箭頭

用CSS繪制三角形箭頭。使用純CSS,你只需要很少的代碼就可以創作出各種瀏覽器都兼容的三角形箭頭!CSS代碼:/* create an arrow that points up */div.arrow-up {width: 0;height: 0;border-left: 5px solid transparent; /* left arrow slant */border-ri…

Jmeter參數化的理解

jmeter參數化有兩種情況: jmeter執行的sql語句中值的參數化(如select過濾條件)csv data set config參數表示方式${zjhm}jmx腳本的設置屬性參數化,方便命令行調用時修改參數(如并發量、執行時間)在腳本中調用…

leetcode216. 組合總和 III(回溯)

找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數&#xff0c;并且每種組合中不存在重復的數字。 說明&#xff1a; 所有數字都是正整數。 解集不能包含重復的組合。 示例 1: 輸入: k 3, n 7 輸出: [[1,2,4]] 代碼 class Solution {List<List…

linux內核epub,Android底層開發技術實戰詳解——內核、移植和驅動(第2版)[EPUB][MOBI][AZW3][42.33MB]...

內容簡介本書從底層原理開始講起&#xff0c;結合真實的案例向讀者詳細介紹了Android內核、移植和驅動開發的整個流程。全書分為21章&#xff0c;依次講解驅動移植的必要性&#xff0c; Goldfish、OMAP內核和驅動解析&#xff0c;顯示系統、輸入系統、振動器系統、音頻系統、視…

機器學習崗位太少_太多的東西要學習,很少的時間

機器學習崗位太少by Rick West由里克韋斯特(Rick West) 太多的東西要學習&#xff0c;很少的時間 (So much to learn, so little time) 我學習&#xff0c;保持動力并實現目標的主要技巧 (My top tips for learning, staying motivated, and achieving your goals) One of the…

用9種辦法解決 JS 閉包經典面試題之 for 循環取 i

2017-01-06Tomson JavaScript轉自 https://segmentfault.com/a/1190000003818163 閉包 1.正確的說,應該是指一個閉包域,每當聲明了一個函數,它就產生了一個閉包域(可以解釋為每個函數都有自己的函數棧),每個閉包域(Function 對象)都有一個 function scope(不是屬性),function s…

bzoj 2296: 【POJ Challenge】隨機種子

Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeDescription1tthinking除了隨機算法&#xff0c;其他什么都不會。但是他還是可以ac很多題目&#xff0c;他用的是什么呢&#xff1f;他會選擇一個好的隨機種子&#xff0c;然后輸出答案。往往他選擇的一個好的種子可…

英特爾第十代處理器為什么不支持win7_5GHz動力澎湃 高主頻多核處理器成就巔峰玩家...

頻率之爭永遠是處理器領域無法回避的話題。高主頻在游戲中所帶來的高速運行&#xff0c;穩定幀數等特性永遠是玩家們所追求的目標。隨著英特爾第十代桌面及移動版酷睿處理器的發布&#xff0c;無論是臺式整機或是筆記本平臺&#xff0c;都已全面進入了5GHz時代。選擇英特爾處理…

leetcode46. 全排列(回溯)

給定一個 沒有重復 數字的序列&#xff0c;返回其所有可能的全排列。 示例: 輸入: [1,2,3] 輸出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 代碼 class Solution {List<List<Integer>> cListnew ArrayList<>();public List<List<…