2012-04-18 65 views
8

我一直在尝试每一种方法,我发现,但我无法做到。我只是想制作一个带圆角的标签,一个带背景图案的阴影。只有当我不想要圆角时,影子才会起作用。我无法让他们在一起!UILabel带圆角,阴影和背景图案

这里是我的代码的影子:

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; 

此代码给我圆润的边角,但没有影子。 有什么建议吗?

谢谢!

+0

http://stackoverflow.com/questions/3690972/why-maskstobounds-yes-prevents-calayer-shadow – 2012-04-19 01:35:51

回答

11

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]; 
+0

这是正确的。问题是用图案图像填充的图层不会尊重角圆角,除非您屏蔽边界,并且如果将_do_掩码限制为边界,则不会绘制阴影。因此阴影需要单独的图层。 – jrturton 2012-04-20 06:06:01

+0

我看到了蓬。但是,不幸的是,这些代码并没有为我显示任何东西。我将代码复制到名为“options.png”的背景文件中,这是我没有的,它显示了一个黑盒子。当我放入我的背景文件名时,它不会显示任何内容。我真的很困惑!我会尽力与它一起玩,看看我能做些什么。 – hsnm 2012-04-21 01:39:01

+0

@ jrturton说的是正确的,这就是我对代码所做的。根据你没有看到任何东西。让我们调试结果 - 试试这个,注释label.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@“options.png”]]; 一行。取消上面的行注释并重命名customLabel变量以标记。您现在应该看到一个黑色阴影的白色圆形标签。正在得到那个结果? – haroldcampbell 2012-04-22 07:39:15