如何将字体设置为我的所有文本字段?

时间:2013-09-11 15:21:52

标签: iphone ios objective-c

我有这个代码并且它正在运行:

[_fieldEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldPassword setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldRegisterName setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldRegisterEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldRegisterPassword setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];

[_titleLogin setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];
[_titleRegister setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];
[_titleVote setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];

我也有这个代码来为我所拥有的每个文本字段应用左侧填充和填充:

UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView    = fieldEmail;

UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView    = fieldPassword;

UIView *fieldRegisterName = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterName.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterName.leftView    = fieldRegisterName;

UIView *fieldRegisterEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterEmail.leftView    = fieldRegisterEmail;

UIView *fieldRegisterPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterPassword.leftView    = fieldRegisterPassword;

但我认为这是一个丑陋的代码。因为我必须逐个设置每个文本字段和标签。是否有更好的方法来简化这个?

非常感谢..

4 个答案:

答案 0 :(得分:3)

制作两个数组:

NSArray* fields= @[ _fieldEmail, _fieldPassword, _fieldRegisterName, _fieldRegisterEmail, _fieldRegisterPassword ];
NSArray* titles = @[ _titleLogin, _titleRegister, _titleVote ];

然后使用makeObjectsPerformSelector:withObject:对所有数组对象执行方法调用:

UIFont* font1= [UIFont fontWithName: @"ABeeZee-Regular" size: 14];
UIFont* font2= [UIFont fontWithName:@"Raleway-ExtraLight" size:28];
[fields makeObjectsPerformSelector: @selector(setFont:) withObject: font1];
[titles makeObjectsPerformSelector: @selector(setFont:) withObject: font2];

至于第二个任务,你可以简单地使用迭代并将创建的UIView对象放在一个可变数组中,如果你以后需要使用它们。如果你想通过名字来调用它们,那么字典也是一个好主意:

NSArray* keys= @[ @"fieldEmail", @"fieldPassword", @"fieldRegisterName", @"fieldRegisterEmail", "fieldRegisterPassword" ];
NSMutableDictionary* fieldsDict= [[NSMutableDictionary alloc] init];
for(NSString* key in keys) {
    UIView* field= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    field.leftViewMode= UITextFieldViewModeAlways;
    field.leftView= field;  // I hope that this property is weak
    [fieldsDict setObject: field forKey: key];
}

答案 1 :(得分:3)

我不这样做。如果检查代码,则TextField有很多类似的模式。

为什么不创建从UITextField继承的customTextField?然后,您需要在customTextField类中设置font,leftViewMode和leftView。使用它时,只需用帧分配。完成!

答案 2 :(得分:3)

在iOS 5及更高版本中,您可以使用UIAppearance API为控件提供一致的样式。您定位的是哪个iOS框架?

OR

根据Brain的建议,您可以创建自定义TextField并使用它而不是UITextField。您的自定义TextField当然会从UITextField继承,但它会更改您想要的不同属性。

答案 3 :(得分:1)

您可以尝试遍历所有子视图,并检查它们是UITextField的实例还是UITextField的任何子类。如果是,则可以设置字体属性 我快速写了一些示例代码,向您展示如何执行此操作:

for (UIView *subview in [[self view] subviews]) {
    if ([subview isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)subview;
        [textField setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14.f]];
    }
}

请注意,此代码仅循环遍历[self view]的“最近”子代,因此不会修改层次结构中更深层次的视图。