iOS - 为 UITextField 实现一个类 [英] iOS - Implementing a Class for UITextField
问题描述
我有一个 UIView
,有很多 UITexfields
,
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];
我可以创建一个 UITextField
类并使用这些参数预设创建此类的实例吗?- 以减少代码:(如果我有很多字段,代码会变得很长且重复?-
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
推荐答案
Yes You Can do this by make a BaseUITextFeild
.h
和 .m代码>文件,如下图所示.
Yes You Can do this by making a BaseUITextFeild
.h
and .m
file as shown in the following image.
然后只需在 BaseUITextFeild.m
文件中添加有关您的设计的以下代码
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]];
}
以编程方式生成自定义 UITextField
然后只需 import "BaseUITextFeild.h"
在 ViewController
中你想使用它的地方.
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];
从界面生成器(Storyboard/.XIB)生成自定义 UITextField
添加一个 UITextfield,然后将类更改为您的自定义 TextView 类.
Generating a custom UITextField From Interface Builder(Storyboard/.XIB)
Add a UITextfield and then just Change the Class to your Custom TextView Class.
通过应用以下技术,您可以在整个应用程序中使用相同的UITextFeild
,只需要import BaseUITextFeild.h
.
By Applying Following technique you can use this same UITextFeild
through out the Application, Just need to import BaseUITextFeild.h
.
你也可以使用这个 UITextFieldCategory
遵循这两个示例 UITextField+withFrame.h 和 UITextField+ColoredPlaceholder.h.您还可以根据需要自定义 UITextField
.
Follow this Two Example UITextField+withFrame.h and UITextField+ColoredPlaceholder.h.
You can also customize you UITextField
as per your need.
这篇关于iOS - 为 UITextField 实现一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!