Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. @implementation TextFieldCell
  2. @synthesize Textfield,placeholder;
  3. - (void)awakeFromNib
  4. {
  5. // Initialization code
  6. Textfield = [self makeTextField];
  7. [self addSubview:Textfield];
  8.  
  9. placeholder = [self PlaceHolderLabel];
  10. [self addSubview:placeholder];
  11.  
  12. }
  13. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  14. {
  15. [super setSelected:selected animated:animated];
  16.  
  17. // Configure the view for the selected state
  18. }
  19. - (UITextField *)makeTextField
  20. {
  21. CGRect TextFrame = CGRectMake(0,0,300,80);
  22. UITextField *textfield = [[UITextField alloc]initWithFrame:TextFrame];
  23. textfield.delegate = self;
  24. textfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
  25. textfield.backgroundColor = [UIColor yellowColor];
  26. textfield.placeholder = [NSString stringWithFormat:@"Mamal"];
  27. UIView *spacerleftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
  28. UIView *spacerrightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
  29. [textfield setLeftViewMode:UITextFieldViewModeAlways];
  30. [textfield setRightViewMode:UITextFieldViewModeAlways];
  31. textfield.leftView = spacerleftView;
  32. textfield.rightView = spacerrightView;
  33.  
  34.  
  35. return textfield;
  36. }
  37. - (UILabel *)PlaceHolderLabel{
  38.  
  39. NSString *labelText = Textfield.placeholder;
  40. Textfield.placeholder = nil;
  41. CGRect labelFrame = CGRectMake(0,0,280,80);
  42. UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
  43. [placeholderLabel setTextColor:[UIColor lightGrayColor]];
  44. [placeholderLabel setText:labelText];
  45. placeholderLabel.textAlignment = NSTextAlignmentRight;
  46. return placeholderLabel;
  47. }
  48. - (UILabel *)clearPlaceHolderLabel{
  49.  
  50. NSString *labelText = nil;
  51. CGRect labelFrame = CGRectMake(0,0,280,80);
  52. UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
  53. [placeholderLabel setText:labelText];
  54. return placeholderLabel;
  55. }
  56. -(void)textFieldDidBeginEditing:(UITextField *)textField {
  57. //Keyboard becomes visible
  58. //perform actions.
  59. NSLog(@"start");
  60. }
  61. -(void)textFieldDidEndEditing:(UITextField *)textField{
  62. NSLog(@"end");
  63. }
  64.  
  65. @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
  66.  
  67. @property (nonatomic,strong) UITableView *table;
  68. @property (weak,nonatomic) UITextField *Textfield;
  69.  
  70. @implementation ViewController
  71. @synthesize table,Textfield;
  72. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  73. {
  74. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  75. if (self) {
  76. // Custom initialization
  77. }
  78. return self;
  79. }
  80.  
  81. - (void)viewDidLoad
  82. {
  83. [super viewDidLoad];
  84. //add table in view
  85. table = [self makeTableView];
  86. [self.view addSubview:table];
  87. [self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
  88.  
  89. //run textfield programmatically
  90. Textfield = [self makeTextField];
  91.  
  92. //hide keyboard with hideKeyboard selector
  93. UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
  94. [self.table addGestureRecognizer:gestureRecognizer];
  95.  
  96. }
  97. - (void) hideKeyboard {
  98. [Textfield resignFirstResponder];
  99. }
  100.  
  101. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
  102. [self.view addGestureRecognizer:tap];
  103.  
  104. - (IBAction)closeKeyboard:(id)sender {
  105. [self.view endEditing:YES];}
  106.  
  107. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  108. {
  109. [self.view endEditing:YES];
  110. }
  111.  
  112. [self.view setEditing:YES];
  113.  
  114. // somewhere in viewDidLoad
  115. UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBackground)];
  116. [self.view addGestureRecognizer:tapRecognizer];
  117. self.view.userInteractionEnabled = YES;
  118. // ...
  119.  
  120. - (void)tapOnBackground
  121. {
  122. [self.view endEditing:YES];
  123. }
  124.  
  125. //Set to Remove Keyboard from view
  126. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
  127. initWithTarget:self
  128. action:@selector(dismissKeyboard)];
  129.  
  130. [self.view addGestureRecognizer:tap];
  131.  
  132. //First Responder Remove Keyboard
  133. -(void)dismissKeyboard {
  134. [Textfield resignFirstResponder];
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement