Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Shadow on UITextField text

Is it possible to add a shadow to the text in a UITextField?

like image 644
DotSlashSlash Avatar asked Aug 13 '09 19:08

DotSlashSlash


People also ask

How do I add a shadow to a text field?

You can add this extension and then use the method "addShadow" to add shadow to you Textfield, label, textview and etc...


2 Answers

As of 3.2, you can use the CALayer shadow properties.

_textField.layer.shadowOpacity = 1.0;    _textField.layer.shadowRadius = 0.0; _textField.layer.shadowColor = [UIColor blackColor].CGColor; _textField.layer.shadowOffset = CGSizeMake(0.0, -1.0); 
like image 157
egarc Avatar answered Sep 18 '22 17:09

egarc


I have a slightly different problem - I want a blurred shadow on a UILabel. Luckily, the solution to this turned out to be number (2) from Tyler

Here's my code :

- (void) drawTextInRect:(CGRect)rect {     CGSize myShadowOffset = CGSizeMake(4, -4);     CGFloat myColorValues[] = {0, 0, 0, .8};      CGContextRef myContext = UIGraphicsGetCurrentContext();     CGContextSaveGState(myContext);      CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();     CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);     CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);      [super drawTextInRect:rect];      CGColorRelease(myColor);     CGColorSpaceRelease(myColorSpace);       CGContextRestoreGState(myContext); } 

This is in a class that extends from UILabel and draws the text with a shadow down and to the right 4px, the shadow is grey at 80% opacity and is sightly blurred.

I think that Tyler's solution number 2 is a little better for performance than Tyler's number 1 - you're only dealing with one UILabel in the view and, assuming that you're not redrawing every frame, it's not a hit in rendering performance over a normal UILabel.

PS This code borrowed heavily from the Quartz 2D documentation

like image 39
deanWombourne Avatar answered Sep 18 '22 17:09

deanWombourne