Is it possible to add a shadow to the text in a UITextField
?
You can add this extension and then use the method "addShadow" to add shadow to you Textfield, label, textview and etc...
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With