1.UILabel
- (void)addLabel
{
//创建一个label
UILabel *pLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 50)];
//内容
pLabel.text = @"HelloWorld\nSecondLine";
//设置字体和大小
pLabel.font = [UIFont fontWithName:@"Verdana" size:18];
//字体对齐方式
pLabel.textAlignment = NSTextAlignmentCenter;
//字体颜色
pLabel.textColor = [UIColor redColor];
//显示行数
pLabel.numberOfLines = 2;
//阴影颜色
pLabel.shadowColor = [UIColor blackColor];
//阴影尺寸
pLabel.shadowOffset = CGSizeMake(2.0,1.0);
//pLabel.shadowOffset = CGSizeMake(6.0,10.0);
//设置label的背景色为透明色
pLabel.backgroundColor = [UIColor clearColor]; //无色的 清除颜色的
[self.view addSubview:pLabel];
[pLabel release];
}
Label 控件,需要注意的几点是:关于多行显示,我们可以使用/n强行换行.或者让label自动换行.但是需要注意一点的是:我们必须设置 label.numberOfLines =? 要不默认可是1行哦~…
2.textField:由于我们在textfield的时候想进行键盘的消失操作以及限制length的操作.所以我们使用了协议进行限制
- (void)addTextField
{
//创建TextField
UITextField *pTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 116, 200, 31)];
//设置边框样式
pTextField.borderStyle = UITextBorderStyleRoundedRect;
//设置字体
pTextField.font = [UIFont systemFontOfSize:18.0];
//根据宽度改变字体大小
pTextField.adjustsFontSizeToFitWidth = YES;
//最小字体
pTextField.minimumFontSize = 2.0;
//清除按钮的的显示时间和样式
pTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//弹出的键盘的样式
pTextField.keyboardType = UIKeyboardTypeDefault;
//设置使用自动更正功能 //减少拼写错误
pTextField.autocorrectionType = UITextAutocorrectionTypeNo;
//设置键盘自动大小写的属性
pTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
//设置返回按钮类型
pTextField.returnKeyType = UIReturnKeyDone;
//设置是否支持密码文本显示
pTextField.secureTextEntry = YES;
//设置委托
pTextField.delegate = self; //设置委托
[self.view addSubview:pTextField];
[pTextField release];
}
#pragma mark-----UITextFieldDelegate------
//弹回键盘方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//放当前的textField放弃第一响应者
[textField resignFirstResponder];
return YES;
}
//限制输入字符串长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//原理:当我们更新了texefield之后,pNewString 就会获得原来的的text,然后刷新
int MAX_LENGTH = 10;
NSMutableString *pNewString = [NSMutableString stringWithString:textField.text];
//完成range内字符串的替换//通过pNewString 指针直接对textfield进行修改
[pNewString replaceCharactersInRange:range withString:string];
//根据两个的长度判断,返回YES or NO.
return ([pNewString length] <= MAX_LENGTH);
}
这里新的知识点是:我们通过协议来限制字符串的长度.而且使用了 replaceCharactersInrange 进行更替.
3.Button
//控制控件
- (void)addButton
{
//创建一个按钮
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置区域
[pBtn setFrame:CGRectMake(10, 70, 100, 40)];
[pBtn setTitle:@"Normal" forState:UIControlStateNormal];
[pBtn setTitle:@"HighLight" forState:UIControlStateHighlighted];
//允许显示高亮
pBtn.showsTouchWhenHighlighted = YES;
[pBtn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[pBtn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pBtn];
}
#pragma mark----------Button SEL--------------
// 按钮的响应方法
-(void)buttonDown:(id)sender
{
NSLog(@"Button pushed down");
}
-(void)buttonRelease:(id)sender
{
NSLog(@"Button released");
}
4.CheckBoxButton 我们只是在点击的时候更换了图片来实现不同的选中和非选中.
//选中框
-(void)addCheckboxButton
{
//创建一个自定义按钮
UIButton *pCheckboxButton = [UIButton buttonWithType:UIButtonTypeCustom];
//设置区域
CGRect checkboxRect = CGRectMake(10,155,36,36);
//pCheckboxButton.backgroundColor = [UIColor blueColor];
//pCheckboxButton.backgroundColor = [UIColor blackColor];
[pCheckboxButton setFrame:checkboxRect];
//分别为两种不同状态设置对应图片
[pCheckboxButton setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];//未选中状态
[pCheckboxButton setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];//选中你状态的下的图片的改变
//添加关联动作
[pCheckboxButton addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pCheckboxButton];
}
#pragma mark-----------checkBtn SEL-------------
- (void)checkboxClick:(UIButton *)btn
{
btn.selected = !btn.selected;
}