iOSLabel实现文字渐变色效果-创新互联
前言

前一段时间公司有需求做文字的的渐变色,自己当时也是网上看了一些,自己写了两个方法,实现了需求,写了很久了,只是现在才想起来,就当继续学习了。分享出来供大家参考学习,下面来看看详细的介绍:
先看看简单的:
- (void)addGradientRampWithColors:(NSArray *)colors text:(NSString *)text {
//label在父视图上的(x,y)的值不是中心点
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];
//这个label是和上面的label是对齐的哦,之前都不好对齐,用这样的方法设置frame就好了
// UILabel *infoTextLabel = [[UILabel alloc] init];
// infoTextLabel.frame = CGRectMake(label.center.x - CGRectGetWidth(label.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 = label.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 = label.layer;
label.frame = gradientLayer.bounds;
}另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻名称:iOSLabel实现文字渐变色效果-创新互联
网站网址:http://lzwzjz.cn/article/cojgii.html


咨询
建站咨询
