UILabel、UIButton、UITextField控件的简单使用

  UILabel、UIButton、UITextField是UI中最简单也是必须掌握的基础控件,下面我来介绍下这3个控件分别用来干嘛的。

  UILabel:是一个文本框,主要用来显示文本信息的,比如微博上看到的文字,就是UILabel。

  UIButton:是一个按钮,用来点击触发一些事件的。比如QQ上发送消息,就是UIButton。

  UITextField:是输入框,用来输入文本信息的。比如QQ上面打字。

  话不多说,直接上代码。我们先来完成UILabel,为了使代码看起来更加美观,我们就把UILabel封装起来,我们先在延展就声明一个方法,

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines font:(UIFont *)font;

  然后我们把这个方法实现出来。

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = textColor;
    label.textAlignment = textAlignment;
    label.numberOfLines = numberOfLines;
    label.font = font;
    return [label autorelease];
}

  这样的话,我们只需要一行代码就可以创建完一个UILabel,然后再写一行代码把UILabel添加到UIView上面。

UILabel *label1 = [self createLabelWithText:@"Hello iOS" frame:CGRectMake(0, 300,100, 40) textColor:[UIColor magentaColor] textAlignment:NSTextAlignmentCenter numberOfLines:0 font:[UIFont boldSystemFontOfSize:21]];
    [containerView addSubview:label1];

  初学者总会忘记设置frame,全部写完了,但是运行起来什么都看不到,那很有可能就是忘记给frame了。

  

  UITextField我们也把它封装起来。

- (UITextField *)createTextFiledWithFrame:(CGRect)frame borderStyle:(UITextBorderStyle)borderStyle placeholder:(NSString *)placeholder secureTextEntry:(BOOL)secureTextEntry keyboardType:(UIKeyboardType)keyboardType {
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.borderStyle = borderStyle;
    textField.placeholder = placeholder;
    textField.secureTextEntry = secureTextEntry;
    textField.keyboardType = keyboardType;
    return [textField autorelease];
}

  按照跟UILabel一样的方法,依样画葫芦,就能完成UITextField的创建了。

  封装起来,看代码是不是简洁性很好的,强迫症的人看起来是不是很舒服。下面,我们就来看看不封装的样子。

// 创建一个UIButton对象,UIButton侧重于交互,响应事件
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 设置frame
    button.frame = CGRectMake(37, 300, 200, 200);
    
    // 设置button显示的文本(标题)
//    [button setTitle:@"八~~~达" forState:UIControlStateNormal];
    
    // 如果button样式是system,那么不给定标题颜色,也能看见标题,默认蓝色
    // 如果button样式custom,那么除了给定标题内容之外,还需要给定标题文本颜色
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    // 设置Button的背景图片
    
    // 设置前景图片时,使用custom样式,如果图片大小大于button的大小,那么图片会被压缩到与button等大,如果图片大小小于button的大小,阿么图片保留原有大小
    // 设置背景图片时,不论使用custom还是system,图片大于或者小于button大小,都会显示的与button等大
    [button setImage:[UIImage imageNamed:@"xigua"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateHighlighted];
    
    // 点击时如果设置了图片,不出现闪烁的效果
//    button.adjustsImageWhenHighlighted = NO;
    
    // 关键方法,为button添加了一个事件
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    [containerView addSubview:button];

  下面是点击事件的代码,不触发什么事件,就输出一下,知道点击事件触发了就好了。

- (void)click:(UIButton *)sender {
    NSLog(@"boom boooooooom ! ! ! !");
}

  这代码是不是看起来就很长了,没有封装起来的代码好看。

  好了,基础的创建已经讲完了,更加深层次就是自己去研究了,建议用纯代码,万行代码的程序员只是一个入门。

 

  

posted on 2016-02-20 22:45  小鸟成长记  阅读(225)  评论(0编辑  收藏  举报

导航