2013-08-24 46 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

使用这就是你想要的形状明确的阴影路径。这听起来像你想要一个与你的文字形状相同的阴影,但更大,每个阴影字母都集中在相应的字母上。一旦你有了,使用阴影半径软化你想要的程度的阴影边缘。

您所使用的代码仅依靠阴影半径使阴影大于文字,这会消除控制柔和度的能力。