2013-08-24 79 views
0

我想達到文本外觀像下面的圖片: enter image description here enter image description here文本用鋒利的陰影圍繞

現在我正在影子周圍的字母,我想不出怎麼做。

我試過到目前爲止:
- 與解決方案:

label.shadowColor = [UIColor blackColor]; 
label.shadowOffset = CGSizeMake(1, 2); 

給出了一個非常清晰的影子,但它並不由兩個方面原因適合我的需要:

  1. 它只給一個影子,由shadowOffset設置,而我需要一個「包裹」陰影;
  2. 此解決方案不會像圖片中那樣提供陰影(漸變)的柔和部分;

與-The解決方案:

label.layer.shadowOffset = CGSizeMake(0, 0); 
label.layer.shadowRadius = 5.0; 
label.layer.shouldRasterize = YES; 
label.layer.shadowOpacity = 1; 
label.layer.shadowColor = [UIColor blackColor].CGColor; 
label.layer.masksToBounds = NO; 

偉大的作品,但它給即使shadowOpacity設置爲1,則shadowColor設置爲黑色太軟陰影:
enter image description here
顯然這還不夠,我已經考慮在標籤的上下文中繪製。但是我不清楚,即使通過背景繪圖,我如何實現目標。

任何想法將不勝感激。

+0

CATextLayer可以幫助你.. – Bala

+0

嗯,我帶你去看看吧,謝謝 – makaron

+0

由於巴拉回答我達到精確我想要的。從表現的角度來看,也許不是完美的方式,但我想不出更好的解決方案。根據任何人的要求,我會提供最終的代碼和圖形結果。 – makaron

回答

1

試試這個 創建自定義UILabel子類,Override以下方法

- (void)drawTextInRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGContextSetShadow(context, CGSizeMake(0.0, 0.0), 10); 
    CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 10, [UIColor blackColor].CGColor); 

    [super drawTextInRect:rect]; 

    CGContextRestoreGState(context); 
} 

,這bottomColorLayer到Label

CALayer *bottomColorLayer = [CALayer layer]; 
bottomColorLayer.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2); 
bottomColorLayer.backgroundColor = [[UIColor colorWithWhite:0 alpha:.5] CGColor]; 
[label.layer insertSublayer:bottomColorLayer above:(CALayer *)label.layer]; 

enter image description here

或者如果你想梯度

CAGradientLayer *bottomGradient = [CAGradientLayer layer]; 
bottomGradient.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2); 
bottomGradient.cornerRadius = 0.0f; 
bottomGradient.colors = @[(id)[[UIColor colorWithWhite:1 alpha:.5] CGColor], (id)[[UIColor colorWithWhite:0 alpha:.5] CGColor]]; 
[label.layer insertSublayer:bottomGradient above:(CALayer *)label.layer]; 
+0

好吧,你不應該擔心字母的顏色,問題只是關於陰影:)但是你的** drawTextInRect **節選給了我主要的推動力。 ** CGContextSetShadowWithColor **使用* shadowOffset *的方式與我提到的相同方式不適用於解決方案,但在* CGContextSetShadowWithColor *中,您可以在一行中應用並繪製結果*幾次*(例如,首先繪製上面的陰影文本,然後在它下面) - 它做到了!萬分感謝! – makaron

+0

我很高興做到這一點:) – Bala

1

嘗試用下面的代碼: -

[_testLable setText:@"TION ERRO"]; 
    [_testLable setTextColor:[UIColor whiteColor]]; 

    [_testLable setFont:[UIFont boldSystemFontOfSize:22] ]; 
    _testLable.layer.shadowOffset = CGSizeMake(0, 0); 
    _testLable.layer.shadowRadius = 10.0; 
    _testLable.layer.shouldRasterize = YES; 
    _testLable.layer.shadowOpacity = 1; 
    _testLable.layer.shadowColor = [UIColor blackColor].CGColor; 
    _testLable.layer.masksToBounds = NO; 

    CGPathRef shadowPath = CGPathCreateWithRect(_testLable.bounds, NULL); 
    _testLable.layer.shadowPath = shadowPath; 
    CGPathRelease(shadowPath); 

它的輸出是這樣的: -

enter image description here

+0

嗯,這基本上使UILabel模糊的背景,因爲你只是從UILabel的邊界獲得路徑。有趣但不是我所需要的。謝謝你的回答! – makaron

1

使用這就是你想要的形狀明確的陰影路徑。這聽起來像你想要一個與你的文字形狀相同的陰影,但更大,每個陰影字母都集中在相應的字母上。一旦你有了,使用陰影半徑軟化你想要的程度的陰影邊緣。

您所使用的代碼僅依靠陰影半徑使陰影大於文字,這會消除控制柔和度的能力。