样式所有UITextFields一次,而不是在每个ViewController?

我试图设置所有的文本框样式:

CGRect frameRect2 = _lastname_field.frame; frameRect2.size.height = 30; _lastname_field.font = [UIFont fontWithName:@"AppleGothic" size:15]; _lastname_field.frame = frameRect2; _lastname_field.backgroundColor= [UIColor whiteColor]; _lastname_field.layer.cornerRadius = 5.0; [_lastname_field.layer setBorderColor: [[UIColor grayColor] CGColor]]; [_lastname_field.layer setBorderWidth: 1.0]; _lastname_field.clipsToBounds = YES; 

这个领域看起来不错。 唯一的问题是,整个应用程序有超过50个文本框。 有没有一种简单的方法来以同样的方式对所有的样式进行devise? 有些甚至还没有被综合/命名。

谢谢!

1)如果你在你的代码中创build你的文件,我会创build一个类似这样的接口的工厂类:

 @interface MyTextFieldFactory : NSObject + (UITextField*)createStyledTextField; @end 

2)如果使用Interface Builder创buildTextField ,则可以在UITextField上编写一个仅实现awakeFromNib: 。 在那里你做所有的定制。 这不需要更改代码,也不需要在nib文件中进行更改。

子类UITextField并在initWithFrame方法中添加您的自定义。 在调用super的自定义类中添加initWithCoder方法,并返回[self initWithFrame:[self frame]]。 然后在Interface Builder中为每个文本字段更改该类。

这将是这样的:

 // MyUITextField.h @interface MyUITextField : UITextField { // Your IVars } @property (retain, readonly) float value; @end // MyUITextField.m @implementation MyClass - (id)initWithCoder:(NSCoder *)decoder { [super initWithCoder:decoder]; return [self initWithFrame:[self frame]]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Your customization } } @end 

你应该可以为所有的属性,除了图层的东西…

在你的AppDelegate中添加一个类似于…的方法

 - (void)changeAppearances { [[UITextField appearance] setFont:[UIFont fontWithName:@"AppleGothic" size:15]]; [[UITextField appearance] setBackgroundColor:[UIColor whiteColor]]; } 

等等。

这将为您的应用程序中的所有UITextField实例设置外观。 (只要你不覆盖他们在你的代码)。

好的,如果外表不起作用,那么下一个最好的办法就是写一个类别来为你做这个。

子类化是可能的,但不是理想的,因为有可能覆盖你不应该的东西(反之亦然)。

将文件添加到您的项目,并select类别types,并将类设置为UITextField。

在类别中添加一个function类似…

 - (void)configureTextFieldWithFrame:(CGRect)frame; 

然后在.m文件中,你可以有…

 - (void)configureTextFieldWithFrame:(CGRect)frame { self.font = [UIFont fontWithName:@"AppleGothic" size:15]; self.frame = frame; self.backgroundColor= [UIColor whiteColor]; self.layer.cornerRadius = 5.0; [self.layer setBorderColor: [[UIColor grayColor] CGColor]]; [self.layer setBorderWidth: 1.0]; self.clipsToBounds = YES; } 

然后你只需要#import UITextField+Configure.h (或者你的分类器被调用。

然后在你的代码中replace你的代码…

 [_lastname_field configureTextFieldWithFrame:frameRect2]; 

这将运行你的类别function。

我同意Fogmeister在代码中创build的文本字段。 但是,如果你在Storyboards中设置文本字段,那么这种方法将不起作用(因为每个字段明确地定义了它的属性)。 但是有一个简单的方法可以工作。

右键单击您的故事板和“打开为…”源代码。 这将SB的XML表示放到编辑器窗口中。 在那里,您可以使用编辑器全局(和/或select性地)更改文本字段属性(或复制到您select的XML编辑器)。

公平的警告,如果你在SB中引入了错误,会导致它不能编译,所以可以杀死你的项目 – 所以要非常小心,并且确保你有SB的备份。 但是如果你在每次更改后都检查,这种技术可以很好地工作。

search“ <textField ”,find如下所示:

 <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="name" minimumFontSize="17" clearButtonMode="whileEditing" id="F9N-Tb-KTd"> <rect key="frame" x="176" y="301" width="472" height="31"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <fontDescription key="fontDescription" type="system" pointSize="14"/> <textInputTraits key="textInputTraits" autocapitalizationType="words" enablesReturnKeyAutomatically="YES"/> <connections> <action selector="changeName:" destination="4" eventType="editingDidEnd" id="bLg-iM-m8a"/> </connections> </textField> 

find一个有你喜欢的fontDescription属性的文本框,一个没有。 然后用你想改变的fontDescription属性replace好的属性。 一定要限制你的改变,如字体,大小和背景。 不要更改id,rect或其他需要对文本字段唯一的内容。

我希望这对你有好处,对我来说,确保我所有的文本字体都具有一致的排版方式是非常方便的。

(要回到正常的视图,“打开为…”Interface Builder – Storyboard)

祝你好运!