<span style="font-size:18px;">
1.UITextField
</span><pre name="code" class="objc"><span style="font-size:18px;"> UITextField:主要作用就是输入和编辑文本.
</span><pre name="code" class="objc"><span style="font-size:18px;"> UITextField的使用:①开辟空间并初始化②设置文本的显示、输入相关的属性.③添加到父视图上,用以显示④释放.
</span>
文本显示的相关属性:
输入控制相关的属性:
外观控制的相关属性:
代码示例:
//创建UITextField
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(40, 40, 200, 40)];
field.backgroundColor = [UIColor whiteColor];
//borderStyle:UITextField的边框样式
field.borderStyle = UITextBorderStyleRoundedRect;
//placeholder占位提示语
field.placeholder = @"请输入密码";
//secureTextEntry:密文输入
field.secureTextEntry = YES;
//keyboardType:修改键盘样式
field.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//代理
field.delegate = self;
//clearButtonMode:清楚模式
field.clearButtonMode = UITextFieldViewModeWhileEditing;
//enabled:是否允许输入(默认YES)
// field.enabled = NO;
//键盘return键的样式(默认return)
field.returnKeyType = UIReturnKeyDone;
//inputview:自定义输入视图(默认键盘)
UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
inputView.backgroundColor = [UIColor cyanColor];
// field.inputView = inputView;
[inputView release];
//inputAccessoryView:输入视图上方的辅助视图(默认为nil)
UIView *inputAccessoryView1 = [[UIView alloc]initWithFrame:CGRectMake(0 , 0, 100, 50)];
inputAccessoryView1.backgroundColor = [UIColor blackColor];
field.inputAccessoryView = inputAccessoryView1;
//一定要设置model才能显示
//leftView(输入框左视图)和leftViewMode(左视图显示模式)这两个属性一起连用
UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
leftview.backgroundColor = [UIColor greenColor];
field.leftView = leftview;
field.leftViewMode = UITextFieldViewModeAlways;
[leftview release];
//
UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
rightView.backgroundColor = [UIColor greenColor];
field.rightView = rightView;
field.rightViewMode = UITextFieldViewModeAlways;
[rightView release];
[view addSubview:field];
[field release];
UITextField *field1 = [[UITextField alloc]initWithFrame:CGRectMake(field.frame.origin.x, field.frame.origin.y + 50, field.frame.size.width, field.frame.size.height)];
field1.borderStyle = UITextBorderStyleRoundedRect;
field1.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
field1.delegate = self;
field1.clearButtonMode = UITextFieldViewModeAlways;
field1.placeholder = @"手机号";
[view addSubview:field1];
[field1 release];
二.UIButton
1.UIButton
2.如何使用UIButton
创建UIButton代码示例
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 100, 100, 100);
button.backgroundColor = [UIColor blackColor];
[backgroundView addSubview:button];
3.为UIButton添加时间
4.UIButton的外观控制
//UIButton代码示例:
-(void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
UIView *backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
backgroundView.backgroundColor = [UIColor whiteColor];
[self.window addSubview:backgroundView];
[backgroundView release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 300, 100);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"登陆" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//selected:默认为NO
[button setTitle:@"gundan" forState:UIControlStateSelected];
button.selected = NO;
//Highlighted:改变文本和颜色
// [button setTitle:@"Coco" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
// [button setBackgroundColor:[UIColor yellowColor]];
// [button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
// [button setBackgroundImage:[UIImage imageNamed:@"2.png"]forState:UIControlStateHighlighted];
// [button setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
//设置圆角
button.layer.cornerRadius = 10;
// //设置边框
button.layer.borderWidth = 0.5;
button.layer.borderColor = [UIColor blackColor].CGColor;
button.tag = 101;
[button addTarget:self action:@selector(aaa:) forControlEvents:UIControlEventTouchUpInside];
[backgroundView addSubview:button];
return YES;
}
-(void)aaa:(UIButton *)button{
UIView *view = [button viewWithTag:101];
UIButton *b = (UIButton *)view;
[b setTitle:@"sdfg" forState:UIControlStateNormal];
// [button setTitle:@"注册" forState:UIControlStateNormal];
NSLog(@"辣死我了");
}