2012-02-22 64 views
1

我需要的标签为 enter image description here添加遮阳标记

我创建了一个标签subclass.Where我写的代码,

- (void)drawRect:(CGRect)rect 
{ 
    [super drawRect:rect]; 

    UIColor * textColor = [UIColor colorWithRed:57.0/255.0 green:100.0/255.0 blue:154.0/255.0 alpha:1.0]; 

    CGContextRef c = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(c, 3.5); 
    CGContextSetLineJoin(c, kCGLineJoinRound); 

    CGContextSetTextDrawingMode(c, kCGTextFillStroke);; 
    self.textColor = [UIColor whiteColor]; 
    [super drawTextInRect:rect]; 

    CGContextSetTextDrawingMode(c, kCGTextFill); 
    self.textColor = textColor; 
// self.shadowColor = [UIColor colorWithRed:11.0/255.0 green:63.0/255.0 blue:126.0/255.0 alpha:1.0]; 
    //self.shadowOffset = CGSizeMake(0.5, -0.5); 
    [super drawTextInRect:rect]; 

} 

通过这个我收到的蓝色文字和白色轮廓但是我需要得到深蓝色的颜色。我怎样才能做到这一点? 请帮帮我。

回答

0

您是否尝试过使用类似的标准石英属性:

label.layer.shadowColor 
label.layer.shadowOffset 

(你需要的QuartzCore框架在您的项目,并导入头)。

+0

Samowski我试过这个,但它没有奏效。 – hgpl 2012-02-22 13:56:03

+0

你是什么意思'没有工作'?效果不是你想要的,或者你不能让它工作? – 2012-02-23 02:54:45

+0

Samowski效果没有出现,因为我想。它影响白色,但我想要淡蓝色的效果。 – hgpl 2012-02-23 05:51:38

1

你应该看看CGContextSetShadowWithColor方法。

CGContextSetShadowWithColor (
      context, 
      shadowSize, 
      shadowBlur, 
      color 
     ); 

我发现了一篇文章,可以帮助您在本网站上:http://majicjungle.com/blog/191/

编辑

下面的代码工作:

- (void)drawRect:(CGRect)rect 
{ 

    UIColor * textColor = [UIColor colorWithRed:57.0/255.0 green:100.0/255.0 blue:154.0/255.0 alpha:1.0]; 

    CGContextRef c = UIGraphicsGetCurrentContext(); 
    //save the context before add shadow otherwise the shadow will appear for both stroke and fill 
    CGContextSaveGState(c); 

    //this is where I add the shadow, and it works 
    CGContextSetShadowWithColor(c, CGSizeMake(2, 2), 3, [[UIColor grayColor] CGColor]); 

    CGContextSetLineWidth(c, 3.5); 
    CGContextSetLineJoin(c, kCGLineJoinRound); 

    CGContextSetTextDrawingMode(c, kCGTextStroke);; 
    self.textColor = [UIColor whiteColor]; 
    [super drawTextInRect:rect]; 

    //restore the context to clear the shadow 
    CGContextRestoreGState(c); 
    CGContextSetTextDrawingMode(c, kCGTextFill); 
    self.textColor = textColor; 
    [super drawTextInRect:rect]; 
} 
+0

适当的效果不会来,请帮助。为此奋斗很多... :( – hgpl 2012-02-23 07:30:05

+0

谢谢弗兰克。但我认为你没有得到我的问题。问题是让文本的阴影不是影子。如果你看标签,那么你会看到有在这个标签的文本中有两个蓝色的阴影。我已经得到了明亮的阴影,现在我想要黑暗的阴影。我可以帮助我解决这个问题,那么我会非常感谢你。:) – hgpl 2012-02-24 05:18:14

+0

你可能是这种意思的梯度。最简单的方法是使用self.textColor = [UIColor colorWithPatternImage:[UIImage imageName:“gradient.png”]];其中,gradient.png是您完成并放置在项目中的图像。 – Franck 2012-02-24 14:07:19

0

Drop Shadow in DrawRect看看。

在接受的答案中有一个开源的链接,根据评论,你可以在那里找到你的答案。它使用drawRect ...