UILabel 上的自定义设置是否值得引入 UILabel 的子类
我有一个标签,我想在几个地方使用它。它无非就是以下自定义设置
label.font = [UIFont fontWithName:@"Arial" size:12.0];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.userInteractionEnabled = YES;
label.textColor = [UIColor whiteColor];
现在我想知道,我是否需要专门为其引入一个(UILabel的)子类?既然它被用在多个地方,那么对于这种用途来说,最好的设计模式是什么?
I have a label that I want to use it in few places. It has nothing more than the following custom settings
label.font = [UIFont fontWithName:@"Arial" size:12.0];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.userInteractionEnabled = YES;
label.textColor = [UIColor whiteColor];
Now I am wondering, do I need to introduce a subclass (of UILabel) specifically for it? Since it's used in multiple places, what would be the best design pattern for this kind of usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,在这种情况下我不会创建子类。您可以做很多事情...您可以创建一个类别。例如在 .h
和 .m 中:
personally, i wouldn't create a subclass in this case. there are a variety of things you can do... you could create a category. e.g. in the .h
and in the .m:
我会在某些可用的类上使用类别或仅使用基本函数。子类化会带来更多的工作。即不断地更改 IB 中的类或更改整个项目中的所有代码。
类别可能如下所示:
您将像这样使用它:
[self.myFirstLabel useMySpecialFormatting];
函数可能如下所示:
您将像这样使用它:
[ClassOrInstanceWithFunction useSpecialFormattingOnLabel:self.myFirstLabel ];
I would use either a category or just a basic function on some available class. Subclassing creates significantly more work. i.e. Constantly having to change the class in IB or changing all the code throughout the project.
Category could look like this:
And you would use it like:
[self.myFirstLabel useMySpecialFormatting];
Function could look like this:
And you would use this like:
[ClassOrInstanceWithFunction useSpecialFormattingOnLabel:self.myFirstLabel];