Objective c 一次设置所有UITextFields的样式,而不是在每个ViewController中?

Objective c 一次设置所有UITextFields的样式,而不是在每个ViewController中?,objective-c,ios,uitextfield,uitextview,Objective C,Ios,Uitextfield,Uitextview,我正在尝试将所有文本字段的样式设置为: 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

我正在尝试将所有文本字段的样式设置为:

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多个文本字段。有没有一种简单的方法可以让它们的风格都一样?其中一些甚至还没有被合成/命名


谢谢

您应该能够对除图层内容之外的所有属性执行此操作

在AppDelegate中添加一个类似于

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


这将设置应用程序中所有UITextField实例的外观。只要不在代码中覆盖它们。

子类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
<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>

好的,如果外观不起作用,那么你的下一个最佳选择就是为你写一个分类

子类化是可能的,但并不理想,因为有可能覆盖不应该覆盖的内容,反之亦然

将文件添加到项目中,选择类别类型并将类设置为UITextField

在类别中添加一个函数,如

- (void)changeAppearances
{
    [[UITextField appearance] setFont:[UIFont fontWithName:@"AppleGothic" size:15]];
    [[UITextField appearance] setBackgroundColor:[UIColor whiteColor]];
}
- (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;
}
然后您只需要导入UITextField+Configure.h或任何您的categoyr

然后在您的代码中,将您的代码替换为

[_lastname_field configureTextFieldWithFrame:frameRect2];

这将运行您的分类功能。

对于代码中创建的文本字段,我同意Fogmeister的观点。但是,如果您在故事板中布置文本字段,这种方法将不起作用,因为每个字段都明确定义了其属性。但有一种简单的方法可以奏效

右键单击故事板并打开为。。源代码。这会在编辑器窗口中显示SB的XML表示。在这里,您可以全局和/或有选择地使用编辑器更改textfield属性,或复制到您选择的XML编辑器

公平的警告,如果你在SB中引入错误,这将阻止它编译,那么你的项目可能会被终止——所以要非常小心,确保你有一个SB的备份。但是,如果您在每次更改后都进行检查,则此技术可以很好地工作


搜索1如果在代码中创建文件,我将创建一个具有如下接口的factory类:

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

2如果使用Interface Builder创建文本字段,则可以在UITextField上编写一个类别,该类别仅实现awakeFromNib:。在这里,您可以进行所有定制。这不需要更改代码,也不需要在nib文件中进行更改。

您可以对其进行子类化以获得图层方法,但我建议对大多数方法使用外观方法。对于iOS来说,这还是相当新的,您可以将我链接到有关子类化的资源吗?我知道如何更改IB中的类,但在您推荐的其他步骤上,我并没有100%地遵循您的建议:响应更新-隐马尔可夫模型。。。图层可能是这里最重要的定制。不确定这是否可以添加一个不需要子类化的新答案。如果可能的话,子类化最好避免使用这样的控件。我们如何从AppDelegate设置图层属性的样式?很高兴知道!不幸的是,我所有的界面布局都在单独的NIB中,没有一个故事板给我们带来GIT问题。我想我可以单独修改每个笔尖?我一直在使用SB和GIT,您可能想重新访问它。至于编辑nib文件,我还没有试过。