一次设置所有UITextField的样式而不是每个ViewController的样式? [英] Style all UITextFields at once rather than in each ViewController?

查看:85
本文介绍了一次设置所有UITextField的样式而不是每个ViewController的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to style all the textfields like so:

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个文本字段。是否有一种简单的方法以同样的方式设置所有这些样式?其中一些甚至没有合成/命名。

That field looks great. Only problem is, there are over 50 textfields throughout the app. Is there an easy way to style all of them the same way? Some of them haven't even been synthesize/named.

谢谢!

推荐答案

1)如果您在代码中创建文件,我会创建一个工厂类,其界面如下:

1) If you create your files within your code, I would create a factory class with an interface like that:

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

2)如果使用Interface Builder创建TextFields ,您可以在UITextField上编写一个类别,它只实现 awakeFromNib:。在那里你做了所有的自定义。这不需要更改代码,也不需要在nib文件中进行更改。

2) If you create your TextFields with the Interface Builder, you could write a category on UITextField, that implements only awakeFromNib:. There you make all your customization. This needs no code changes, neither does it need changes within the nib files.

这篇关于一次设置所有UITextField的样式而不是每个ViewController的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆