国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS Label實(shí)現(xiàn)文字漸變色效果

2020-07-26 02:48:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

前一段時(shí)間公司有需求做文字的的漸變色,自己當(dāng)時(shí)也是網(wǎng)上看了一些,自己寫了兩個(gè)方法,實(shí)現(xiàn)了需求,寫了很久了,只是現(xiàn)在才想起來(lái),就當(dāng)繼續(xù)學(xué)習(xí)了。分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹:

先看看簡(jiǎn)單的:

- (void)addGradientRampWithColors:(NSArray *)colors text:(NSString *)text { //label在父視圖上的(x,y)的值不是中心點(diǎn) CGPoint point = CGPointMake(30, 500); UILabel *label = [[UILabel alloc]init]; label.text = text; label.font = [UIFont systemFontOfSize:20]; // label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; //label的中心和想象的一樣啦!! label.center = CGPointMake(point.x + CGRectGetWidth(label.bounds)/2, point.y - CGRectGetHeight(label.bounds)/2); [self.view addSubview:label]; //這個(gè)label是和上面的label是對(duì)齊的哦,之前都不好對(duì)齊,用這樣的方法設(shè)置frame就好了// UILabel *infoTextLabel = [[UILabel alloc] init];// infoTextLabel.frame = CGRectMake(label.center.x - CGRectGetWidth(label.bounds)/2 ,point.y + 30, 220, 50);// infoTextLabel.text = @"你說(shuō)的是哦";// infoTextLabel.font = [UIFont systemFontOfSize:20];// infoTextLabel.backgroundColor =[UIColor redColor];// infoTextLabel.numberOfLines = 0;// infoTextLabel.textAlignment = NSTextAlignmentLeft;// infoTextLabel.textColor = [UIColor blueColor];// [infoTextLabel sizeToFit];// [self.view addSubview:infoTextLabel]; //在后面添加漸變圖層 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = label.frame; gradientLayer.colors = colors; //漸變的方向(0,0) (0,1) (1,0)(1,1)為四個(gè)頂點(diǎn)方向 //(I.e. [0,0] is the bottom-left // corner of the layer, [1,1] is the top-right corner.) The default values // are [.5,0] and [.5,1] gradientLayer.startPoint = CGPointMake(0, 1); gradientLayer.endPoint = CGPointMake(1, 1); [self.view.layer addSublayer:gradientLayer]; gradientLayer.mask = label.layer; label.frame = gradientLayer.bounds;}

自己覺(jué)得這樣的方法用起來(lái)不是很方便,所以接下來(lái)是另一種方法:

.m文件

@implementation CFGradientLabel- (void)drawRect:(CGRect)rect { CGSize textSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}]; CGRect textRect = (CGRect){0, 0, textSize}; // 畫(huà)文字(不做顯示用, 主要作用是設(shè)置 layer 的 mask) CGContextRef context = UIGraphicsGetCurrentContext(); [self.textColor set]; [self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL]; // 坐標(biāo)(只對(duì)設(shè)置后的畫(huà)到 context 起作用, 之前畫(huà)的文字不起作用) CGContextTranslateCTM(context, 0.0f, rect.size.height - (rect.size.height - textSize.height) * 0.5); CGContextScaleCTM(context, 1.0f, -1.0f); CGImageRef alphaMask = CGBitmapContextCreateImage(context); CGContextClearRect(context, rect); // 清除之前畫(huà)的文字 // 設(shè)置mask CGContextClipToMask(context, rect, alphaMask); // 畫(huà)漸變色 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, NULL); CGPoint startPoint = CGPointMake(textRect.origin.x,          textRect.origin.y); CGPoint endPoint = CGPointMake(textRect.origin.x + textRect.size.width,         textRect.origin.y + textRect.size.height); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); // 釋放內(nèi)存 CGColorSpaceRelease(colorSpace); CGGradientRelease(gradient); CFRelease(alphaMask);}

.h 文件

@interface CFGradientLabel : UILabel@property(nonatomic, strong) NSArray* colors;@end

接下來(lái)是調(diào)用的方法,修改了一下的

- (void)addGradientLabelWithFrame:(CGPoint)point gradientText:(NSString *)text infoText:(NSString *)infoText colors:(NSArray *)colors font:(UIFont *)font { static NSInteger labelTag = 200; CFGradientLabel *lable = [[CFGradientLabel alloc] init]; lable.text = text; lable.font = font; lable.tag = labelTag; lable.textAlignment = NSTextAlignmentCenter; [lable sizeToFit]; //之前項(xiàng)目的時(shí)候設(shè)置了為0,忘了注釋,所以有的小伙伴用的時(shí)候就不顯示了……(^-^)// lable.alpha = 0; lable.center = point; lable.colors = colors; [self.view addSubview:lable];}

做的是引導(dǎo)頁(yè),看看效果圖如下:


總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江口县| 南皮县| 本溪| 明光市| 哈尔滨市| 云林县| 昆山市| 丰原市| 司法| 洛阳市| 麻阳| 衡阳县| 湾仔区| 田阳县| 吴忠市| 塔城市| 正镶白旗| 临海市| 济宁市| 蚌埠市| 泸水县| 弥勒县| 措勤县| 岢岚县| 宜章县| 乡城县| 德安县| 江达县| 开远市| 江北区| 沙湾县| 宕昌县| 延长县| 广元市| 拜城县| 句容市| 怀集县| 沙洋县| 郁南县| 马关县| 东阿县|