2、UILabel、UIButton和UITextField基础

UILabel

修改字体大小,默认 16

    label.font = [UIFont systemFontOfSize:20];

设置字体类型

    label.font = [UIFont fontWithName:@"DIN Condensed" size:50];

文本颜色

    label.textColor = [UIColor redColor];

设置行数  当赋值0时,将自动换行 默认1 设置时最多设置2行,否则用0

    label.numberOfLines = 0;

文本显示位置

    label.textAlignment = NSTextAlignmentCenter;

背景颜色

    label.backgroundColor = [UIColor orangeColor];

自动根据文本调整高度,前提是label的尺寸要大于显示字体的尺寸

    [label sizeToFit];

 

UIButton

button设置三种状态下的背景图片

直接设置图片,会将标题挤到一边

button的状态顺序:正常->高亮->选中->正常

[button setBackgroundImage:[UIImage imageNamed:@"图片名称"] forState:UIControlStateNormal/UIControlStateHighlighted/UIControlStateSelected];

 

设置按钮标题

    [button setTitle:@"停止" forState:UIControlStateNormal];

让button的标题偏移( 一般只向左右偏移)

[button setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];

设置标题的字体大小

button.titleLabel.font = [UIFont  boldSystemFontOfSize:20];

为按钮添加事件

    [button addTarget:self action:@selector(buttonAct:) forControlEvents:UIControlEventTouchUpInside];

 

设置按钮的选中状态

   button.selected = NO;

设置按钮是否可用

   button.enabled = NO;

设置是否响应触摸事件 默认YES

    button.userInteractionEnabled = NO;

 

UITextField

设置背景颜色

   textField.backgroundColor = [UIColor redColor];

设置输入边框的样式

    textField.borderStyle = UITextBorderStyleRoundedRect;

设置首字母大写

    textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;

设置单词提示

    textField.autocorrectionType = UITextAutocorrectionTypeYes;

设置文本字体

    textField.font = [UIFont systemFontOfSize:30];

 

设置字体颜色

    textField.textColor = [UIColor redColor];

设置内容提示

    textField.placeholder = @"请输入密码";

设置安全文本输入

    textField.secureTextEntry = YES;

设置键盘类型

    textField.keyboardType = UIKeyboardTypeDefault;

设置修改return键

    textField.returnKeyType = UIReturnKeyJoin;

添加清除按钮

    textField.clearButtonMode = UITextFieldViewModeWhileEditing;

设置成为第一响应者(初始时弹出键盘)

   [textField becomeFirstResponder];

设置代理(UITextFieldDelegate)

    textField.delegate = self;

代理方法如下:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

//    将要开始编辑时调用
    return YES;
}       


- (void)textFieldDidBeginEditing:(UITextField *)textField{

//    已经开始编辑时调用
}         

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

//    将要结束编辑时调用
    return YES;
}          

- (void)textFieldDidEndEditing:(UITextField *)textField{

//    结束编辑时调用
}          

 

posted @ 2016-04-12 20:29  C_David  阅读(183)  评论(0编辑  收藏  举报