0

我有这个代码,它正在工作:

[_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

4 回答 4

3

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

或者

正如 Brain 所建议的那样,您可以创建一个自定义 TextField 并使用它来代替 UITextField。您的自定义 TextField 当然会从 UITextField 继承,但它会更改您想要的不同属性。

于 2013-09-11T15:43:43.473 回答
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];
}
于 2013-09-11T15:34:13.587 回答
3

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

为什么不创建继承自 UITextField 的 customTextField?然后,您需要在 customTextField 类中设置字体、leftViewMode 和 leftView。使用时,只需用框架分配即可。完毕!

于 2013-09-11T15:38:29.127 回答
1

您可以尝试遍历所有子视图并检查它们是否UITextFieldUITextField. 如果是,您可以设置字体属性。
我很快写了一些示例代码来向您展示如何做到这一点:

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],因此不会修改层次结构中更深层次的视图。

于 2013-09-11T15:30:03.353 回答