- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 0, 0)];
label.font = [UIFont boldSystemFontOfSize:30];
label.text = @"Your Name";
[label sizeToFit];
UIImage *gradientImage = [self.class gradientImageWithSize:label.frame.size beginColor:UIColor.redColor endColor:UIColor.yellowColor];
label.textColor = [UIColor colorWithPatternImage:gradientImage];
[self.view addSubview:label];
}
+ (UIImage *)gradientImageWithSize:(CGSize)size beginColor:(UIColor *)beginColor endColor:(UIColor *)endColor
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(__bridge id)beginColor.CGColor, (__bridge id)endColor.CGColor];
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(gradientLayer.bounds.size);
[gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
---
I want to set gradient color to the text in UILabel but find that if the text is non-english language, the gradient color truncated. Images below will show the case. Is this a bug?
https://welkin-xie.oss-cn-shenzhen.aliyuncs.com/1.png
https://welkin-xie.oss-cn-shenzhen.aliyuncs.com/2.png
https://welkin-xie.oss-cn-shenzhen.aliyuncs.com/3.png
---
Emmm, non-english can not be post here, I make a html with the text in the images. Just copy them and replace the line of
label.text = @"Your Name";
https://welkin-xie.oss-cn-shenzhen.aliyuncs.com/gradient-1.html