方法類
#import "Healp.h" @implementation Healp
- colors 漸變的顏色
- locations 漸變顏色的分割點
- startPoint&endPoint 顏色漸變的方向,范圍在(0,0)與(1.0,1.0)之間,如(0,0)(1.0,0)代表水平方向漸變,(0,0)(0,1.0)代表豎直方向漸變
/* view 是要設置漸變字體的控件 bgVIew是view的父視圖 colors是漸變的組成顏色 startPoint是漸變開始點 endPoint結束點 */ +(void)TextGradientview:(UIView *)view bgVIew:(UIView *)bgVIew gradientColors:(NSArray *)colors gradientStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{ CAGradientLayer* gradientLayer1 = [CAGradientLayer layer]; gradientLayer1.frame = view.frame; gradientLayer1.colors = colors; gradientLayer1.startPoint =startPoint; gradientLayer1.endPoint = endPoint; [bgVIew.layer addSublayer:gradientLayer1]; gradientLayer1.mask = view.layer; view.frame = gradientLayer1.bounds; } /* control 是要設置漸變字體的控件 bgVIew是control的父視圖 colors是漸變的組成顏色 startPoint是漸變開始點 endPoint結束點 */ +(void)TextGradientControl:(UIControl *)control bgVIew:(UIView *)bgVIew gradientColors:(NSArray *)colors gradientStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{ CAGradientLayer* gradientLayer1 = [CAGradientLayer layer]; gradientLayer1.frame = control.frame; gradientLayer1.colors = colors; gradientLayer1.startPoint =startPoint; gradientLayer1.endPoint = endPoint; [bgVIew.layer addSublayer:gradientLayer1]; gradientLayer1.mask = control.layer; control.frame = gradientLayer1.bounds; }
引用
/*方法1*/
UILabel* testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 400, 50)];
testLabel.text = @"label上漸變方法1";
testLabel.font = [UIFont systemFontOfSize:30];
[self.view addSubview:testLabel];
[Healp TextGradientview:testLabel bgVIew:self.view gradientColors:@[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor] gradientStartPoint:CGPointMake(0, 1) endPoint:CGPointMake(1, 1)];
/*方法2*/
_label=[[UILabel alloc]initWithFrame:CGRectMake(10, 300, self.view.frame.size.width-20,50)];
[self.view addSubview:_label];
_label.text=@"label上漸變方法2";
_label.textAlignment=NSTextAlignmentCenter;
_label.font = [UIFont systemFontOfSize:30];
_label.textColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"gradient"]];
_btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 350, self.view.frame.size.width-20, 100)];
[self.view addSubview:_btn];
_btn.titleLabel.font=[UIFont systemFontOfSize:30];
_btn.titleLabel.numberOfLines=0;
[_btn setTitle:@"button上字體漸變色設置" forState:UIControlStateNormal];
[Healp TextGradientControl:_btn bgVIew:self.view gradientColors:@[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor] gradientStartPoint:CGPointMake(0, 1) endPoint:CGPointMake(1, 1)];
