In my project, I use UITextField, which is embedded in a plain white UIButton with rounded corners to create a “pretty” text box. This code is used several times in different views, so I decided to create a custom UIView for this. Code for this custom UIView:
BSCustomTextField.h
#import <Foundation/Foundation.h>
@interface BSCustomTextField : UIView {
UIButton* btnTextFieldContainer;
UITextField* textField;
NSString* text;
}
@property (retain, nonatomic) UIButton* btnTextFieldContainer;
@property (retain, nonatomic) UITextField* textField;
@property (retain, nonatomic) NSString* text;
-(id)initWithPosition:(CGPoint)position;
@end
BSCustomTextField.m
#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>
@implementation BSCustomTextField
@synthesize btnTextFieldContainer, textField, text;
-(id)initWithPosition:(CGPoint)position{
if (self == [super init]) {
btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.returnKeyType = UIReturnKeySend;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.];
[btnTextFieldContainer addSubview:textField];
[self addSubview:btnTextFieldContainer];
}
return self;
}
-(void)dealloc{
[btnTextFieldContainer release];
[textField release];
[super dealloc];
}
@end
Thus, when using this view in the container's viewDidLoad view (see the code below), the view is properly displayed in the desired position and looks exactly as indicated, but it does not respond to touch events and thus does not become the first responder when touched .
the code:
searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder = NSLocalizedString(@"lsUserName", @"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];
[searchTextField.textField FirstResponder] .
, BSCustomTextField inline :
UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];
searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.clearButtonMode = UITextFieldViewModeAlways;
searchTextField.returnKeyType = UIReturnKeySend;
searchTextField.keyboardType = UIKeyboardTypeEmailAddress;
searchTextField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.];
searchTextField.placeholder = NSLocalizedString(@"lsUserName", @"");
[btnTextFieldContainer addSubview:searchTextField];
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer];
[btnTextFieldContainer release];
, , . searchTextField . , UIView UIBiew UIButton UITextFiled. , , .
,
Roman