网络整合的两种方案,经验证有效:
效果图如下:
方法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,快来看啊";
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);
// 创建渐变层
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:
先创建一个基于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};
// 画文字(不做显示用 主要作用是设置layer的mask)
CGContextRef context = UIGraphicsGetCurrentContext();
[self.textColor set];
[self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL];
// 坐标 (只对设置后的画到context起作用 之前画的文字不起作用)
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);// 清除之前画的文字
// 设置mask
CGContextClipToMask(context, rect, alphaMask);
// 画渐变色
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);
// 释放内存
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
CFRelease(alphaMask);
}
@end
调用方法:
- (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);
}
效果图如上图。