文字的颜色渐变
在实际的项目中为了项目的美观,设计师会设计一些渐变的颜色,抽空研究了一下。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self addGradientRampWithColors:@[(id)[UIColor blueColor].CGColor,(id)[UIColor blackColor].CGColor] text:@"渐变色咯,哈哈哈哈哈哈哈哈哈哈"];
}
- (void)addGradientRampWithColors:(NSArray *)colors text:(NSString *)text {
//label在父视图上的(x,y)的值不是中心点
//也可直接设置frame 这样更方便一点
CGPoint point = CGPointMake(30, 500);
UILabel *lable = [[UILabel alloc]init];
lable.text = text;
lable.font = [UIFont systemFontOfSize:20];
// lable.textAlignment = NSTextAlignmentCenter;
[lable sizeToFit];
//lable的中心和想象的一样啦!!
lable.center = CGPointMake(point.x + CGRectGetWidth(lable.bounds)/2, point.y - CGRectGetHeight(lable.bounds)/2);
[self.view addSubview:lable];
//这个label是和上面的label是对齐的哦,之前都不好对齐,用这样的方法设置frame就好了
UILabel *infoTextLabel = [[UILabel alloc] init];
infoTextLabel.frame = CGRectMake(lable.center.x - CGRectGetWidth(lable.bounds)/2 ,point.y + 30, 220, 50);
infoTextLabel.text = @"你说的是哦";
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 = lable.frame;
gradientLayer.colors = colors;
//渐变的方向(0,0) (0,1) (1,0)(1,1)为四个顶点方向
//(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 = lable.layer;
lable.frame = gradientLayer.bounds;
}