为重用的 UILabels 创建一个类
我发现自己在我的应用程序中重复创建了本质上相同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文件->新文件...-> 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
感谢 Adubr 的推动,以下工作得以实现:
Thanks to the nudge from Adubr, the following works:
你不需要在这里上课。只需定义像
addLabel(UINavigationBar *navBar, ... text)
这样的函数并使用它。You don't need a class here. Just define function like
addLabel(UINavigationBar *navBar, ... text)
and use it.这是用什么语言创建的? (对我来说,看起来有点像 XAML/WPF...或者 FLEX?)
一般来说,语法如下:
但也取决于编程语言,也可能取决于平台。 :) 此外,如果您将其创建为外部类并将其公开,那么您应该能够在需要时调用这些函数。
例如,可以在不调用导航栏标签的情况下创建导航栏...然后用户可以决定是否为这个新标签输入一些内容。如果他们没有输入任何内容,程序将继续执行下一个方法,或者用户创建一个新标签。
它将是一个动态且更具互动性的系统。 :)
希望有帮助。
What language are creating this in? (Looks abit like XAML/WPF to me...or FLEX?)
Generally speaking the syntax would be as follows:
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.