gpt4 book ai didi

ios - 带有圆角、投影和背景图案的 UILabel

转载 作者:可可西里 更新时间:2023-11-01 03:07:53 26 4
gpt4 key购买 nike

我一直在尝试我找到的每一种方法,但我无法做到。我只是想制作一个带有圆角的标签,一个带有背景图案的阴影。阴影仅在我不想要圆角时才有效。我不能把它们都放在一起!

这是我的带阴影的代码:

label.text = msg;
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(20,10,280,40);
label.backgroundColor
= [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"msg_box_bg.png"]];

[label.layer setCornerRadius:10];
[label.layer setMasksToBounds:NO];

/* Shadow */
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOpacity = 0.6;
label.layer.shadowOffset = CGSizeMake(0,0);
label.layer.shadowRadius = 3;

这给了我没有圆角的阴影。但是如果我使用

[label.layer setMasksToBounds:YES];

这将给我没有阴影的圆角。我听取了使用影子路径的建议,所以带有影子路径的代码如下所示:

label.text = msg;
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(20,10,280,40);
label.backgroundColor
= [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"msg_box_bg.png"]];

[label.layer setCornerRadius:10];
[label.layer setMasksToBounds:YES];

/* Shadow */
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOpacity = 0.6;
label.layer.shadowOffset = CGSizeMake(0,0);
label.layer.shadowRadius = 3;
label.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:label.frame cornerRadius:10]CGPath];
label.layer.shouldRasterize = YES;

这段代码确实给了我圆角但没有阴影。有什么建议吗?

谢谢!

最佳答案

enter image description here

我使用下面的代码得到了你想要的结果。

CGSize size = CGSizeMake(280, 40);

/** Shadow */
CALayer *shadowLayer = [CALayer new];
shadowLayer.frame = CGRectMake(20,100,size.width,size.height);
shadowLayer.cornerRadius = 10;

shadowLayer.backgroundColor = [UIColor clearColor].CGColor;
shadowLayer.shadowColor = [UIColor blackColor].CGColor;
shadowLayer.shadowOpacity = 0.6;
shadowLayer.shadowOffset = CGSizeMake(0,0);
shadowLayer.shadowRadius = 3;

/** Label */
UILabel *label = [UILabel new];
label.text = @"Hello World";
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(0, 0, size.width, size.height);
label.backgroundColor = [UIColor clearColor];
label.layer.cornerRadius = 10;
[label.layer setMasksToBounds:YES];
// customLabel.backgroundColor = [UIColor whiteColor];
label.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"options.png"]];

/** Add the Label to the shawdow layer */
[shadowLayer addSublayer:label.layer];

[self.view.layer addSublayer:shadowLayer];

[shadowLayer release];

关于ios - 带有圆角、投影和背景图案的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201180/

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