UILabel 上的自定义设置是否值得引入 UILabel 的子类

发布于 2024-12-23 00:24:26 字数 360 浏览 0 评论 0原文

我有一个标签,我想在几个地方使用它。它无非就是以下自定义设置

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-12-30 00:24:26

就我个人而言,在这种情况下我不会创建子类。您可以做很多事情...您可以创建一个类别。例如在 .h

@interface UILabel (MyLabel)

+ (UILabel *)createMyLabelWithFrame;

@end

和 .m 中:

@implementation UILabel (MyLabel)

+ (UILabel *)createMyLabelWithFrame:(CGRect)frame {
  UILabel *label = [[UILabel alloc] initWithFrame:frame];
  label.font = [UIFont fontWithName:@"Arial" size:12.0];
  label.textAlignment = UITextAlignmentCenter;
  label.backgroundColor = [UIColor clearColor];
  label.userInteractionEnabled = YES;
  label.textColor = [UIColor whiteColor];
  return label;
}

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

@interface UILabel (MyLabel)

+ (UILabel *)createMyLabelWithFrame;

@end

and in the .m:

@implementation UILabel (MyLabel)

+ (UILabel *)createMyLabelWithFrame:(CGRect)frame {
  UILabel *label = [[UILabel alloc] initWithFrame:frame];
  label.font = [UIFont fontWithName:@"Arial" size:12.0];
  label.textAlignment = UITextAlignmentCenter;
  label.backgroundColor = [UIColor clearColor];
  label.userInteractionEnabled = YES;
  label.textColor = [UIColor whiteColor];
  return label;
}
如歌彻婉言 2024-12-30 00:24:26

我会在某些可用的类上使用类别或仅使用基本函数。子类化会带来更多的工作。即不断地更改 IB 中的类或更改整个项目中的所有代码。

类别可能如下所示:

@implementation UILabel (FormatMyLabels)
-(void)useMySpecialFormatting{
    self.font = [UIFont fontWithName:@"Arial" size:12.0];
    self.textAlignment = UITextAlignmentCenter;
    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = YES;
    self.textColor = [UIColor whiteColor]; 
}
@end

您将像这样使用它:

[self.myFirstLabel useMySpecialFormatting];

函数可能如下所示:

-(void)useSpecialFormattingOnLabel:(UILabel *)label{
    label.font = [UIFont fontWithName:@"Arial" size:12.0];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    label.textColor = [UIColor whiteColor];
}

您将像这样使用它:

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

@implementation UILabel (FormatMyLabels)
-(void)useMySpecialFormatting{
    self.font = [UIFont fontWithName:@"Arial" size:12.0];
    self.textAlignment = UITextAlignmentCenter;
    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = YES;
    self.textColor = [UIColor whiteColor]; 
}
@end

And you would use it like:

[self.myFirstLabel useMySpecialFormatting];

Function could look like this:

-(void)useSpecialFormattingOnLabel:(UILabel *)label{
    label.font = [UIFont fontWithName:@"Arial" size:12.0];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    label.textColor = [UIColor whiteColor];
}

And you would use this like:

[ClassOrInstanceWithFunction useSpecialFormattingOnLabel:self.myFirstLabel];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文