问题
I have a UIView, with lots of UITexfields,
UITextField *field1 = [[UITextField alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
field1.backgroundColor = [UIColor clearColor];
field1.borderStyle = UITextBorderStyleNone;
field1.returnKeyType = UIReturnKeyNext;
field1.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
field1.font = [UIFont fontWithName:inputFont size:fontSize];
field1.placeholder = @"size";
[self addSubview:field1];
Can I create a UITextField Class and create an instance of this class with these parameters preset? -so as to reduce the code: (If I have lots of fields the code gets very long and repetitive? -
Can i get some help on how about to set up this class! -thank you
回答1:
Yes You Can do this by making a BaseUITextFeild .h and .m file as shown in the following image.
And then Just add the following code regarding your design in BaseUITextFeild.m file
- (void)awakeFromNib {
[self setupUI];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
// Function to setup the common UI throught the app.
- (void)setupUI {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
Generating a custom UITextField programmatically
And then just import "BaseUITextFeild.h" in the ViewController Where you want to use it.
BaseUITextFeild *field1 = [[BaseUITextFeild alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
[self addSubview:field1];
Generating a custom UITextField From Interface Builder(Storyboard/.XIB)
Add a UITextfield and then just Change the Class to your Custom TextView Class.
By Applying Following technique you can use this same UITextFeild through out the Application, Just need to import BaseUITextFeild.h.
You can also use this UITextFieldCategory
Follow this Two Example UITextField+withFrame.h and UITextField+ColoredPlaceholder.h.
You can also customize you UITextField as per your need.
回答2:
Create a method
-(UITextField*)createTextFieldWithFrame:(CGRect)frame{
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.backgroundColor = [UIColor clearColor];
textField.borderStyle = UITextBorderStyleNone;
textField.returnKeyType = UIReturnKeyNext;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
textField.placeholder = @"size";
[self.view addSubview:textField];
return textField;
}
for creating multiple textfields call this method
UITextField *text1 = [self createTextFieldWithFrame:CGRectZero];
UITextField *text2 = [self createTextFieldWithFrame:CGRectZero];
回答3:
You can create a custom xib with the UITextField created as you want and reuse that every time you need.
Or you can subclass UITextField and do the additional setup inside.
来源:https://stackoverflow.com/questions/17505395/ios-implementing-a-class-for-uitextfield