網(wǎng)絡(luò)整合的兩種方案堡距,經(jīng)驗(yàn)證有效:
效果圖如下:
方法1:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor yellowColor].CGColor];
[self functionGradientLayerWithColors:colors];
}
- (void)functionGradientLayerWithColors:(NSArray *)colors{
UILabel* lbl = [[UILabel alloc] init];
lbl.text = @"我是有漸變色的Label,快來(lái)看啊";
lbl.font = [UIFont systemFontOfSize:23];
[lbl sizeToFit];
[self.view addSubview:lbl];
lbl.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.3);
// 創(chuàng)建漸變層
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = lbl.frame;
gradientLayer.colors = colors;
gradientLayer.startPoint = CGPointMake(0, 1);
gradientLayer.endPoint = CGPointMake(1, 1);
[self.view.layer addSublayer:gradientLayer];
gradientLayer.mask = lbl.layer;
lbl.frame = gradientLayer.bounds;
}
方法2:
先創(chuàng)建一個(gè)基于UILabel的類:JJGradientLabel
JJGradientLabel.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface JJGradientLabel : UILabel
@property(nonatomic, strong) NSArray* colors;
@end
JJGradientLabel.m
#import "JJGradientLabel.h"
@implementation JJGradientLabel
- (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 = NULL;
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);
}
@end
調(diào)用方法:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor yellowColor].CGColor];
[self functionGradientLabelWithColors:colors];
}
- (void)functionGradientLabelWithColors:(NSArray *)colors{
JJGradientLabel* lbl = [[JJGradientLabel alloc] init];
lbl.text = @"我是漸變色的呀呀呀呀--label";
lbl.font = [UIFont systemFontOfSize:23];
[lbl sizeToFit];
lbl.colors = colors;
[self.view addSubview:lbl];
lbl.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.4);
}
效果圖如上圖易稠。