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
Thanks to the nudge from Adubr, the following works: