Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UILabel上的自定义设置是否值得引入UILabel的子类_Iphone_Design Patterns_Uilabel - Fatal编程技术网

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

Iphone UILabel上的自定义设置是否值得引入UILabel的子类,iphone,design-patterns,uilabel,Iphone,Design Patterns,Uilabel,我有一个标签,我想在几个地方使用它。它只有以下自定义设置 label.font = [UIFont fontWithName:@"Arial" size:12.0]; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; label.userInteractionEnabled = YES; label.textColor = [UIColor whiteColor];

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

label.font = [UIFont fontWithName:@"Arial" size:12.0];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.userInteractionEnabled = YES;
label.textColor = [UIColor whiteColor];

现在我想知道,我是否需要为它引入一个子类(UILabel)?由于它被用于多个地方,对于这种用途来说,什么样的设计模式才是最好的呢?

我会在一些可用的类上使用一个类别或只是一个基本函数。子类化会大大增加工作量。i、 e.在整个项目中,必须不断更改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
-(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];
}
你会像这样使用它:

[self.myFirstLabel使用mySpecialFormatting]

函数可以如下所示:

@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
-(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、 e.在整个项目中,必须不断更改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
-(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];
}
你会像这样使用它:

[self.myFirstLabel使用mySpecialFormatting]

函数可以如下所示:

@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
-(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]

就我个人而言,在这种情况下我不会创建子类。你可以做很多事情。。。您可以创建一个类别。e、 g.在.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;
}

就个人而言,我不会在这种情况下创建子类。你可以做很多事情。。。您可以创建一个类别。e、 g.在.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;
}