为重用的 UILabels 创建一个类

发布于 2024-11-07 09:45:21 字数 1131 浏览 1 评论 0原文

我发现自己在我的应用程序中重复创建了本质上相同的 UILabel。所以我认为现在是为其创建一个类并调用该类的好时机?

例如,我多次重复使用此代码来为视图中的 UINavigationBar 创建 UILabel,唯一的区别是我认为可以作为参数传递的 label.text:

// - - - - - Add a Navigation Bar

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack; 
navBar.translucent = YES;

[self.view addSubview:navBar];
[navBar release];

// - - - - - Add a label to the navbar

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
navLabel.text = @"Some Label String";
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont boldSystemFontOfSize:20];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.textAlignment = UITextAlignmentCenter;
[navBar addSubview:navLabel];
[navLabel release];

所以我认为我应该能够创建一个 UILabelNavBar 类并通过像这样调用它来简单地创建 UILabel:

navBar addSubview:[UILabelNavBar createLabel:@"Some Label String"]];

除非我不知道如何创建该类。 任何帮助表示赞赏。 lq

I find myself creating essentially the same UILabel repeatedly in my app. So I'm thinking this is a good time to create a class for it and just call the class?

For example, I reuse this code many times to create a UILabel for my UINavigationBar in views, with the only difference being the label.text that I'm thinking I can pass as a parameter:

// - - - - - Add a Navigation Bar

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack; 
navBar.translucent = YES;

[self.view addSubview:navBar];
[navBar release];

// - - - - - Add a label to the navbar

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
navLabel.text = @"Some Label String";
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont boldSystemFontOfSize:20];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.textAlignment = UITextAlignmentCenter;
[navBar addSubview:navLabel];
[navLabel release];

So I'm thinking I should be able to create a UILabelNavBar Class and simply create the UILabel by calling it like this:

navBar addSubview:[UILabelNavBar createLabel:@"Some Label String"]];

Except I don't know how to create the class.
Any help is appreciated.
lq

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

夜光 2024-11-14 09:45:21

文件->新文件...-> Objective C 类-> UIView子类->输入班级名称->选择文件夹->将刚刚创建的 .m 和 .h 文件中的 UIView 更改为 UILabel

File -> New file... -> Objective C class -> UIView subclass -> enter the name of the class -> choose folder -> change UIView in just created .m and .h files to UILabel

猫九 2024-11-14 09:45:21

感谢 Adubr 的推动,以下工作得以实现:

// *** OftenUsedLabel.h ***

#import <UIKit/UIKit.h>

@interface OftenUsedLabel : UILabel 
{
    UILabel *oftenUsedLabel;
}

@property (nonatomic, retain) UILabel *oftenUsedLabel;

@end

// *** OftenUsedLabel.m ***

#import "OftenUsedLabel.h"

@implementation OftenUsedLabel

@synthesize oftenUsedLabel;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        CGRect rect;
        rect = CGRectMake(0.0,0.0,280.0,30.0);

        oftenUsedLabel = [[UILabel alloc] initWithFrame:rect];
        oftenUsedLabel.backgroundColor = [UIColor clearColor];
        oftenUsedLabel.textColor = [UIColor whiteColor];
        oftenUsedLabel.font = [UIFont boldSystemFontOfSize:20];
        oftenUsedLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        oftenUsedLabel.textAlignment = UITextAlignmentCenter;

        [self addSubview:oftenUsedLabel];
    }

    return self;    
}

- (void)dealloc {
    [oftenUsedLabel release];
    [super dealloc];
}


// *** SomeViewController.m ***

#import "OftenUsedLabel.h"

// CALL THE LABEL LIKE THIS:

    CGRect rect;
    rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
    UILabel *myLabel = [[[OftenUsedLabel alloc] initWithFrame:rect] autorelease];
    myLabel.text = @"Some Text";
    [self.view addSubview:myLabel];
    [myLabel release];

Thanks to the nudge from Adubr, the following works:

// *** OftenUsedLabel.h ***

#import <UIKit/UIKit.h>

@interface OftenUsedLabel : UILabel 
{
    UILabel *oftenUsedLabel;
}

@property (nonatomic, retain) UILabel *oftenUsedLabel;

@end

// *** OftenUsedLabel.m ***

#import "OftenUsedLabel.h"

@implementation OftenUsedLabel

@synthesize oftenUsedLabel;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        CGRect rect;
        rect = CGRectMake(0.0,0.0,280.0,30.0);

        oftenUsedLabel = [[UILabel alloc] initWithFrame:rect];
        oftenUsedLabel.backgroundColor = [UIColor clearColor];
        oftenUsedLabel.textColor = [UIColor whiteColor];
        oftenUsedLabel.font = [UIFont boldSystemFontOfSize:20];
        oftenUsedLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        oftenUsedLabel.textAlignment = UITextAlignmentCenter;

        [self addSubview:oftenUsedLabel];
    }

    return self;    
}

- (void)dealloc {
    [oftenUsedLabel release];
    [super dealloc];
}


// *** SomeViewController.m ***

#import "OftenUsedLabel.h"

// CALL THE LABEL LIKE THIS:

    CGRect rect;
    rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
    UILabel *myLabel = [[[OftenUsedLabel alloc] initWithFrame:rect] autorelease];
    myLabel.text = @"Some Text";
    [self.view addSubview:myLabel];
    [myLabel release];
溺渁∝ 2024-11-14 09:45:21

你不需要在这里上课。只需定义像 addLabel(UINavigationBar *navBar, ... text) 这样的函数并使用它。

You don't need a class here. Just define function like addLabel(UINavigationBar *navBar, ... text) and use it.

╄→承喏 2024-11-14 09:45:21

这是用什么语言创建的? (对我来说,看起来有点像 XAML/WPF...或者 FLEX?)

一般来说,语法如下:

Class NavBar()
{

   function navBar()
   {
      //new navbar object
      UINavigationBar *navBar = [[UINavigationBar alloc] 
      initWithFrame:CGRectMake(0, 0,          320, 44)];
      navBar.barStyle = UIBarStyleBlack; 
      navBar.translucent = YES;

      [self.view addSubview:navBar];
      [navBar release];

      //call the navbar function and create the label for the new navbar
      navBarLabel()

   }

   function navBarLabel()
   {

      UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
      navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      navLabel.text = @"Some Label";
      navLabel.backgroundColor = [UIColor clearColor];
      navLabel.textColor = [UIColor whiteColor];
      navLabel.font = [UIFont boldSystemFontOfSize:20];
      navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
      navLabel.textAlignment = UITextAlignmentCenter;
      [navBar addSubview:navLabel];
      [navLabel release];         

   }

}

但也取决于编程语言,也可能取决于平台。 :) 此外,如果您将其创建为外部类并将其公开,那么您应该能够在需要时调用这些函数。

例如,可以在不调用导航栏标签的情况下创建导航栏...然后用户可以决定是否为这个新标签输入一些内容。如果他们没有输入任何内容,程序将继续执行下一个方法,或者用户创建一个新标签。

它将是一个动态且更具互动性的系统。 :)

希望有帮助。

What language are creating this in? (Looks abit like XAML/WPF to me...or FLEX?)

Generally speaking the syntax would be as follows:

Class NavBar()
{

   function navBar()
   {
      //new navbar object
      UINavigationBar *navBar = [[UINavigationBar alloc] 
      initWithFrame:CGRectMake(0, 0,          320, 44)];
      navBar.barStyle = UIBarStyleBlack; 
      navBar.translucent = YES;

      [self.view addSubview:navBar];
      [navBar release];

      //call the navbar function and create the label for the new navbar
      navBarLabel()

   }

   function navBarLabel()
   {

      UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
      navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      navLabel.text = @"Some Label";
      navLabel.backgroundColor = [UIColor clearColor];
      navLabel.textColor = [UIColor whiteColor];
      navLabel.font = [UIFont boldSystemFontOfSize:20];
      navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
      navLabel.textAlignment = UITextAlignmentCenter;
      [navBar addSubview:navLabel];
      [navLabel release];         

   }

}

But depends on the programming language and possibly platform too. :) Also if you create it as an external class and make it public, you should be able to call these functions whenever you need them.

So for example the navBar could be created without calling the navBar label...then the user can decide to input something for this new label or not. If they do not input anything the program goes on to the next method, or the user creates a new label.

It will be a dynamic and more interactive system. :)

Hope that helps.

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