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

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

代码如下所示:

- (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的错误。
标签没有英文字符时,磁贴的大小会出错。

如果要遮罩标签上的渐变颜色,只需对标签使用遮罩视图。

物镜

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


迅速

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)
}


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

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

10-12 14:45