Skip to content

Instantly share code, notes, and snippets.

@adeel
Created July 28, 2011 19:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeel/1112262 to your computer and use it in GitHub Desktop.
Save adeel/1112262 to your computer and use it in GitHub Desktop.
UITextField category for conveniently adding text fields inside frames with padding.
//
// UITextField+withFrame.h
//
#import <Foundation/Foundation.h>
@interface UITextField (withFrame)
+ (UITextField *)textFieldWithFrame:(CGRect)frame
font:(UIFont *)font;
+ (UITextField *)textFieldWithFrame:(CGRect)frame
font:(UIFont *)font
padding:(CGFloat)padding;
@end
//
// UITextField+withFrame.m
//
#import "UITextField+withFrame.h"
@implementation UITextField (withFrame)
// Create a text field inside the frame with 8pt of horizontal padding.
+ (UITextField *)textFieldWithFrame:(CGRect)frame
font:(UIFont *)font {
return [UITextField textFieldWithFrame:frame font:font padding:8.0f];
}
+ (UITextField *)textFieldWithFrame:(CGRect)frame
font:(UIFont *)font
padding:(CGFloat)padding {
UITextField *field = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
field.font = font;
[field sizeToFit];
field.frame = CGRectMake(
frame.origin.x + 5.0f, frame.origin.y + (frame.size.height - field.frame.size.height) / 2.0f,
frame.size.width - 10.0f, field.frame.size.height);
return field;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment