How to add text shadows to a UITextView?
ios, objective-c, quartz-graphics, uitextview
Solution
@adali's answer will work, but its wrong. You shouldn't add the shadow to the `UITextView` itself in order to effect the visible views inside. As you can see, by applying the shadow to the `UITextView` the cursor will also have the shadow.
The approach that should be used is with `NSAttributedString`.
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:textView.font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:textView.textColor range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
textView.attributedText = attString;
However `textView.attributedText` is for iOS6. If you must support lower versions, you could use the following approach.
CALayer *textLayer = (CALayer *)[textView.layer.sublayers objectAtIndex:0];
textLayer.shadowColor = [UIColor whiteColor].CGColor;
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f);
textLayer.shadowOpacity = 1.0f;
textLayer.shadowRadius = 0.0f;
Problem
I've been searching around to find an easy way to add shadows to the text of a UITextView, like you can do in a UILabel. I found this question where there was an answer that supposedly does this, however, it makes no sense why this should be the case. Question: Adding shadows to the layer of the UITextView itself should not affect the text inside, rather it should shadow the entire object, right? In my case, even adding the shadow to the layer of the textview is not having any effect (even after adding the QuartzCore headers).