虽然现在大部分都是用SB或者xib结合代码来写项目,但是也还有很多人在用存代码,刚开始写微博先分享些简单的东西,
有时候在一个页面用存代码的情况下可能会创建很多个UIlable或者,VIew,UITextfilede,那么怎么能避免重复创建造成代码量过多呢。
为什么不在创建tabbar的地方先把这些写成一个方法呢,在需要的页面进行调用 。上代码:
//创建label
- (UILabel *)createALabelWithframe:(CGRect)frame andWithName:(NSString *)name andTextAlcent:(NSTextAlignment)textAlent andTextColor:(UIColor *)color andFont:(UIFont *)font{
UILabel * label = [[UILabel alloc]initWithFrame:frame];
label.text = name;
label.textAlignment = textAlent;
label.textColor = color;
label.font = font;
return label;
}
//调用
UILabel * titleLabel = [self createALabelWithframe:CGRectMake(Screen_Width*0.05 + image1.size.width , Screen_Height*0.02, Screen_Width *0.3, Screen_Height *0.03) andWithName:title andTextAlcent:NSTextAlignmentLeft andTextColor:UIColorFromRGB(0x333333) andFont:[UIFont systemFontOfSize:16]];
[self.contentView addSubview:titleLabel];
//创建view
- (UIView *)createViewWithfram:(CGRect)fram andColor:(UIColor *)background
{
UIView *view = [[UIView alloc]initWithFrame:fram];
view.backgroundColor = background;
return view;
}
一般vie用到的也就这两个属性,所以不多写,调用更Lable类似
//创建textfield
- (UITextField *)createTextfiledWithFram:(CGRect)fram andTextColor:(UIColor *)textColor andFont:(UIFont *)font andkey:(UIKeyboardType)key
andplaceholder:(BOOL)secureTextEntry andPlacehoder:(NSString *)text
{
UITextField *textfield = [[UITextField alloc]initWithFrame:fram];
textfield.textColor = textColor;
textfield.font = font;
textfield.secureTextEntry = secureTextEntry;//设置输入框是否明文
textfield.placeholder = text;//设置提示语
return textfield;
}
调用同上
虽然这些都是简单的东西但是得收集起来并分享出来,若有其他需求欢迎评论