Created
September 28, 2014 07:53
-
-
Save sdpjswl/053fc131f09b9838e49d to your computer and use it in GitHub Desktop.
UITextField subclass to add inner shadow, text and placeholder insets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GolopoTextField.h | |
// Golopo | |
// | |
// Created by Esense-15 on 05/09/14. | |
// Copyright (c) 2014 esense. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface GolopoTextField : UITextField | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GolopoTextField.m | |
// Golopo | |
// | |
// Created by Esense-15 on 05/09/14. | |
// Copyright (c) 2014 esense. All rights reserved. | |
// | |
#import "GolopoTextField.h" | |
@implementation GolopoTextField | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Initialization code | |
[self applyShadow]; | |
} | |
return self; | |
} | |
-(void)applyShadow { | |
self.layer.cornerRadius = 3.0f; | |
[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.3]]; | |
CAShapeLayer* shadowLayer = [CAShapeLayer layer]; | |
[shadowLayer setFrame:self.bounds]; | |
// Standard shadow stuff | |
[shadowLayer setShadowColor:[[UIColor colorWithWhite:0 alpha:0.3] CGColor]]; | |
[shadowLayer setShadowOffset:CGSizeMake(-2.0f, 2.0f)]; | |
[shadowLayer setShadowOpacity:1.0f]; | |
[shadowLayer setShadowRadius:1]; | |
// Causes the inner region in this example to NOT be filled. | |
[shadowLayer setFillRule:kCAFillRuleEvenOdd]; | |
// Create the larger rectangle path. | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathAddRect(path, NULL, CGRectInset(self.bounds, -42, -42)); | |
// Add the inner path so it's subtracted from the outer path. | |
// someInnerPath could be a simple bounds rect, or maybe | |
// a rounded one for some extra fanciness. | |
CGPathRef someInnerPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:3.0f].CGPath; | |
CGPathAddPath(path, NULL, someInnerPath); | |
CGPathCloseSubpath(path); | |
[shadowLayer setPath:path]; | |
CGPathRelease(path); | |
[self.layer addSublayer:shadowLayer]; | |
CAShapeLayer* maskLayer = [CAShapeLayer layer]; | |
[maskLayer setPath:someInnerPath]; | |
[shadowLayer setMask:maskLayer]; | |
} | |
// placeholder position | |
- (CGRect)textRectForBounds:(CGRect)bounds { | |
return CGRectInset(bounds, 5, 5); | |
} | |
// text position | |
- (CGRect)editingRectForBounds:(CGRect)bounds { | |
return CGRectInset(bounds, 5, 5); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment