Does custom settings on UILabel worth introducing a subclass of UILabel - iphone

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?

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;
}

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];

Related

UILabel not align center on UINavigationBar in iPhone

I need to align UILabel text center on UINavigationBar. I have implemented all the steps but some how its not center Align. Below is code.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTextColor:[UIColor whiteColor]];
self.titleLabel.text = title;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.titleLabel.adjustsFontSizeToFitWidth = NO;
self.titleLabel.adjustsLetterSpacingToFitWidth = NO;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.titleLabel sizeToFit];
float titlePadding = 60.0f;
self.titleLabel.left = titlePadding;
self.titleLabel.width = self.width - 2 * titlePadding;
self.titleLabel.top = ceilf(self.frame.size.height / 2 - self.titleLabel.frame.size.height / 2);
Please help me with this
Try This It will help
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// ^-Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"PageThreeTitle", #"");
[label sizeToFit];

How to add A scrollView for vertical scrolling allong with labels

I want add UIScrollView with label and make it scroll vertical if i have more labels i am adding following code but it is not scrolling. I have more 5 labels to add in it also.
In Header file
IBOutlet UIScrollView*scrollView;
#property(nonatomic,retain) IBOutlet UIScrollView*scrollView;
In implementation file in viewDidLoad
titleLabel = [[UILabel alloc ] initWithFrame:CGRectMake(892,257,206,84)];
titleLabel.textAlignment = UITextAlignmentRight;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel];
titleLabel = [[UILabel alloc ] initWithFrame:CGRectMake(10,20,206,84)];
titleLabel.textAlignment = UITextAlignmentRight;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel];
titleLabel1 = [[UILabel alloc ] initWithFrame:CGRectMake(10,110,206,84)];
titleLabel1.textAlignment = UITextAlignmentRight;
titleLabel1.textColor = [UIColor blackColor];
titleLabel1.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel1.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel1];
titleLabel2 = [[UILabel alloc ] initWithFrame:CGRectMake(10,190,206,84)];
titleLabel2.textAlignment = UITextAlignmentRight;
titleLabel2.textColor = [UIColor blackColor];
titleLabel2.text=#"tilte Working Fine";
titleLabel2.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel2];
titleLabel3 = [[UILabel alloc ] initWithFrame:CGRectMake(10,270,206,84)];
titleLabel3.textAlignment = UITextAlignmentRight;
titleLabel3.textColor = [UIColor blackColor];
titleLabel3.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel3.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel3];
titleLabel4 = [[UILabel alloc ] initWithFrame:CGRectMake(10,360,206,84)];
titleLabel4.textAlignment = UITextAlignmentRight;
titleLabel4.textColor = [UIColor blackColor];
titleLabel4.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel4.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel4];
titleLabel5 = [[UILabel alloc ] initWithFrame:CGRectMake(10,450,206,84)];
titleLabel5.textAlignment = UITextAlignmentRight;
titleLabel5.textColor = [UIColor blackColor];
titleLabel5.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel5.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel5];
titleLabel6 = [[UILabel alloc ] initWithFrame:CGRectMake(10,530,206,84)];
titleLabel6.textAlignment = UITextAlignmentRight;
titleLabel6.text=#"tilte Working Fine";
titleLabel6.textColor = [UIColor blackColor];
titleLabel6.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel6];
You need to increase the content size of the scroll view its diffrent from its frame size if content size is more then frame size it will scroll.

How to do parameter passing for Creating N number or labels programmatically

I am doing an application where I am creating a LABELS programmatically..What my Requirement is I need to create some 10 LABELS programmatically, so only there Position and Text label changes. Instead of Writing the same function for every Label Thought of doing it By parameter passing, where only v need pass only the Label name and Position of the label...how to implement it?
//Below code shows creation of Labels With Different Names, instead I want to have one function and do parameter passing rather than creating a function for each label.
-(void)Vendorname
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,80,150,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"Vendor Name";
[self.view addSubview:myLabel];
[myLabel release];
}
-(void)address
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,110,350,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"84-21 Gwanheon dong,jungo-gu,seoul,south korea";
[self.view addSubview:myLabel];
[myLabel release];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
return [myLabel autorelease];
}
[self.view addSubview:[self createLabel:CGRectMake(50,80,150,20):#"ur Title"]];
-(void)createLabel:(NSString *)text :(CGRect)frame{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = text;
[self.view addSubview:myLabel];
[myLabel release];
}
call the above method...
[self createLabel:#"label1" :frame];
try this one
int x=50;
int y=80
[self.view addSubview:[self createLabel:CGRectMake(x,y,150,20) title:#"Vendore"]];
-(UILabel*)createLabel:(CGRect)frame title:(NSString*)title{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = title;
return [myLabel autorelease];
}

Style UIlabel text like the font in the Settings panel

How do you style a UILabel text like the text below (created using titleForFooterInSection)?
Use the below , you might need to alter the values of color and Size, Try with different-2 combination of both..
UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0,1);
See the UILabel Documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor colorWithRed:(76.0f/255.0f) green:(86.0f/255.0f) blue:(108.0f/255.0f) alpha:1.0f];

Customizing Title in the NavigationBar for a ABPeoplePickerNavigationController

I have created an AddressBook kind of application where I display a list of people in a UITableView, when a person is selected an ABUnknownPersonViewController is pushed. From this ABUnknownPersonViewController the user is able (by using the built in functionality) to either add the person to a "New Contact" or "Existing Contact".
This is where my problem is located. I am using a Custom UILabel for the NavigationBar title throughout my application. And I need to be able to do this for the views that are pushed by "Add New Contact" / "Add to Existing Contact" as well.
I solved this for the "Add New Contact" by creating a category for ABNewPersonViewController, but the same approach doesn't work for the "Add to Existing Contact". I guess this might be due to the fact that is it an ABPersonPickerNavigationController that is pushed.
#implementation ABPeoplePickerNavigationController (customTitle)
-(void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.tintColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
}
The change of color for the NavigationBar's tintColor works fine, but I cant find a proper way to access the title. Help that include a working example where you are able to change the title in a ABPeoplePickerNavigationController that is pushed from a ABUnknownPersonViewController would be much appriciated.
This is how the category for ABNewPersonViewController (that works) looks like:
#implementation ABNewPersonViewController (customTitle)
-(void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
}
-(void)setTitle:(NSString *)theTitle {
CGRect frame = CGRectMake(0, 0, 45, 45);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:255.0 alpha:1];
label.shadowOffset = CGSizeMake(1, 1);
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithRed:23/255.0 green:23/255.0 blue:23/255.0 alpha:1];
self.navigationItem.titleView = label;
label.text = theTitle;
[label sizeToFit];
}
#end
I use custom labels for this purpose. You can also change font size with this code.
Example:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 320 / 11)];
label.font = 16.0;
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.text = #"Your title";
self.navigationItem.titleView = label;
[label release];
Hope this helps.