Is it possible to add a shadow to the text in a UITextField
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
I don't think you get built-in support for text shadows here, the way you do with
UILabel
.Two ideas:
(1) [Moderately tricky to code.] Add a second
UITextField
behind the original, at a very small offset (maybe by (0.2,0.8)? ). You can listen to every text change key-by-key by implementing thetextField:shouldChangeCharactersInRange:replacementString:
method in theUITextFieldDelegate
protocol. Using that, you can update the lower text simultaneously. You could also make the lower text (the shadow text) gray, and even slightly blurry using the fact that fractionally-offset text rects appear blurry. Added: Oh yea, don't forget to set the top text field's background color to[UIColor clearColor]
if you go with this idea.(2) [Even more fun to code.] Subclass
UITextField
and override thedrawRect:
method. I haven't done this before, so I'll mention up front that this depends on this being the designated drawing method, and it may turn out that you have to override another drawing function, such asdrawTextInRect:
, which is specific toUITextField
. Now set up the drawing context to draw shadows via theCGContextSetShadow
functions, and call[super drawRect:rect];
. Hopefully that works -- in case the originalUITextField
code clears the drawing context's shadow parameters, that idea is hosed, and you'll have to write the whole drawing code yourself, which I anti-recommend because of all the extras that come withUITextFields
like copy-and-paste and kanji input in Japanese.As of 3.2, you can use the CALayer shadow properties.
Although the method of applying the shadow directly to the
UITextView
will work, it's the wrong way to do this. By adding the shadow directly with a clear background color, all subviews will get the shadow, even the cursor.The approach that should be used is with
NSAttributedString
.However textView.attributedText is for iOS6. If you must support lower versions, you could use the following approach. (Dont forget to add
#import <QuartzCore/QuartzCore.h>
)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 :
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