Make Multiple Label and Button in a single line
Using this code we can make label and button , no need to write code again and again.
//MakeLabel
-(id)makeLabelWithTag:(int)tag andX:(CGFloat)x Andy:(CGFloat)y andWidth:(CGFloat)width andHeight:(CGFloat)height andText:(NSString *)Text andFontName:(NSString *)fontName andFontSize:(CGFloat)fontSize andFontColor:(UIColor *)color
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
label.text = Text;
label.tag=tag;
label.backgroundColor = [UIColor clearColor];
label.textColor = color;
NSString *fontName2 = @"verdana";
label.font = [UIFont fontWithName:fontName2 size:fontSize];
[self.view addSubview:label];
return self;
}
//Make Button
-(id)makeBtnWithTag:(int)tag andX:(CGFloat)x Andy:(CGFloat)y andWidth:(CGFloat)width andHeight:(CGFloat)height andImageName:(NSString *)imageName { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; CGRect homeframe = CGRectMake(x, y, width, height); [btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside]; [btn setFrame:homeframe]; btn.tag = tag; [self.view addSubview:btn]; return self; }
//BtnPressEvent
-(IBAction)btnPress:(id)sender
{
UIButton *btn = (UIButton*)sender;
switch (btn.tag)
{
case 0:
NSLog(@"btn with tag 0 press");
break;
case 1:
NSLog(@"btn with tag 1 press");
break;
default:
break;
}
}
Comments
Post a Comment