gpt4 book ai didi

ios - 渐变颜色在非英语语言的 UILabel 中被截断

转载 作者:行者123 更新时间:2023-11-28 19:25:59 26 4
gpt4 key购买 nike

我想为 UILabel 中的文本设置渐变颜色,但发现如果文本是非英语语言,渐变颜色会被截断。这是错误吗?

images

代码如下:

- (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;
}

只需将 label.text 替换为:

label.text = @"你的名字";

Or

label.text = @"君の名は";

然后它发生了。有谁知道为什么吗?

最佳答案

我认为这是 UIKit 的错误。当标签没有英文字符时,图 block 的大小会出错。

如果你想屏蔽标签上的渐变颜色,只需为你的标签使用屏蔽 View 。

objective-c

- (void)viewDidLoad {
[super viewDidLoad];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 0, 0)];
label.font = [UIFont boldSystemFontOfSize:30];
label.text = @"君の名は";
[label sizeToFit];


UIView *gradientView = [[UIView alloc] initWithFrame:label.frame];
label.frame = gradientView.bounds;

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = gradientView.bounds;
gradientLayer.colors = @[(__bridge id)UIColor.redColor.CGColor, (__bridge id)UIColor.yellowColor.CGColor];
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);

[gradientView.layer addSublayer:gradientLayer];
[gradientView addSubview:label];
[gradientView setMaskView:label];
}

swift

override func viewDidLoad() {
super.viewDidLoad()

let label = UILabel(frame: CGRect(x: 10, y: 100, width: 0, height: 0))
label.text = "君の名は"
label.font = UIFont.boldSystemFont(ofSize: 30)
label.sizeToFit()

let gradientView = UIView(frame: label.frame)
label.frame = gradientView.bounds
let gradientLayer = CAGradientLayer()
gradientLayer.frame = gradientView.bounds
gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)

gradientView.layer.addSublayer(gradientLayer)
gradientView.addSubview(label)
gradientView.mask = label

self.view.addSubview(gradientView)
}

enter image description here

关于ios - 渐变颜色在非英语语言的 UILabel 中被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com