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

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

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

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

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

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

- (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上面。blog

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的建立了。get

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

// 建立一个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 ! ! ! !");
}

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

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

相关文章
相关标签/搜索