2012-04-18 66 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