Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation TextFieldCell
- @synthesize Textfield,placeholder;
- - (void)awakeFromNib
- {
- // Initialization code
- Textfield = [self makeTextField];
- [self addSubview:Textfield];
- placeholder = [self PlaceHolderLabel];
- [self addSubview:placeholder];
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated
- {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- - (UITextField *)makeTextField
- {
- CGRect TextFrame = CGRectMake(0,0,300,80);
- UITextField *textfield = [[UITextField alloc]initWithFrame:TextFrame];
- textfield.delegate = self;
- textfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
- textfield.backgroundColor = [UIColor yellowColor];
- textfield.placeholder = [NSString stringWithFormat:@"Mamal"];
- UIView *spacerleftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
- UIView *spacerrightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
- [textfield setLeftViewMode:UITextFieldViewModeAlways];
- [textfield setRightViewMode:UITextFieldViewModeAlways];
- textfield.leftView = spacerleftView;
- textfield.rightView = spacerrightView;
- return textfield;
- }
- - (UILabel *)PlaceHolderLabel{
- NSString *labelText = Textfield.placeholder;
- Textfield.placeholder = nil;
- CGRect labelFrame = CGRectMake(0,0,280,80);
- UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
- [placeholderLabel setTextColor:[UIColor lightGrayColor]];
- [placeholderLabel setText:labelText];
- placeholderLabel.textAlignment = NSTextAlignmentRight;
- return placeholderLabel;
- }
- - (UILabel *)clearPlaceHolderLabel{
- NSString *labelText = nil;
- CGRect labelFrame = CGRectMake(0,0,280,80);
- UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
- [placeholderLabel setText:labelText];
- return placeholderLabel;
- }
- -(void)textFieldDidBeginEditing:(UITextField *)textField {
- //Keyboard becomes visible
- //perform actions.
- NSLog(@"start");
- }
- -(void)textFieldDidEndEditing:(UITextField *)textField{
- NSLog(@"end");
- }
- @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
- @property (nonatomic,strong) UITableView *table;
- @property (weak,nonatomic) UITextField *Textfield;
- @implementation ViewController
- @synthesize table,Textfield;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //add table in view
- table = [self makeTableView];
- [self.view addSubview:table];
- [self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
- //run textfield programmatically
- Textfield = [self makeTextField];
- //hide keyboard with hideKeyboard selector
- UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
- [self.table addGestureRecognizer:gestureRecognizer];
- }
- - (void) hideKeyboard {
- [Textfield resignFirstResponder];
- }
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
- [self.view addGestureRecognizer:tap];
- - (IBAction)closeKeyboard:(id)sender {
- [self.view endEditing:YES];}
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self.view endEditing:YES];
- }
- [self.view setEditing:YES];
- // somewhere in viewDidLoad
- UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBackground)];
- [self.view addGestureRecognizer:tapRecognizer];
- self.view.userInteractionEnabled = YES;
- // ...
- - (void)tapOnBackground
- {
- [self.view endEditing:YES];
- }
- //Set to Remove Keyboard from view
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
- initWithTarget:self
- action:@selector(dismissKeyboard)];
- [self.view addGestureRecognizer:tap];
- //First Responder Remove Keyboard
- -(void)dismissKeyboard {
- [Textfield resignFirstResponder];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement